Unity3D 控制物体移动、旋转、缩放
发布日期:2021-05-10 09:22:37 浏览次数:27 分类:精选文章

本文共 4148 字,大约阅读时间需要 13 分钟。

Unity Transform���������������������������������

���Unity������������������Transform���������������������������������������������������������������������������������������Transform���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

������Transform������������������

Transform���������Unity���������������������������������������������������������������������������script���������transform������������������������������������������������������������������������������������������������������������

  • ������������������

    ������������������������������������������������������������transform.Translate������������������������������������������

    //������������
    float TranslateSpeed = 10f;
    //������������������
    transform.Translate(Vector3.forward * TranslateSpeed);

    ���������Vector3Initialization������������������������������������������������Vector3.forward������������������Vector3.left������������������

  • ���������������

    ������������������������������������������������x������z���������������������

    //x���������������
    float xSpeed = -5f;
    //z���������������
    float zSpeed = 10f;
    //������������x���y���z���
    transform.Translate(xSpeed, 0, zSpeed);

    ������������xSpeed���zSpeed������������������������������������������������

  • ������������

    ������������������������������������������������������������������������transform.position���������������

    //x���������
    float xPosition = -5f;
    //z���������
    float zPosition = 10f;
    //������������������(xPosition, 0, zPosition)
    transform.position = Vector3(xPosition, 0, zPosition);

    ���������������������������������������������������������

  • ���������������������

    ���������������������������������������������������������������������������������������Unity������������Input������������������������������������������������������������������������������������������������������������������������������������������

  • ������������

    ������������������������������������������������������������������������������KeyCode.W���������W���������������������������������

    //W������������
    if (Input.GetKey(KeyCode.W)) {
    print("W������");
    }
  • ������������

    ���������������������������������������������������������������������������������������������Input.GetMouseButton(0)���������������������

    //������������������
    if (Input.GetMouseButton(0)) {
    print("������������");
    }
    //������������������������
    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");
  • ���������������

    ���������������pad������������������������Input.GetAxis������������������������������������������������������������������������-1���1������������������������������������

    //���������������������
    float speed = 100.0f;
    //���������������
    float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    //���������������
    float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    //������������
    transform.position += new Vector3(x, 0, z);
  • ���������������������

    ������������������������������������������������������Transform������������Rotate���Scale������������������������������

  • ������������

    ���������������������Vector3���Quaternion������������������������������������������Y������������

    //���Y���������
    transform.Rotate(Vector3.up * Time.deltaTime * speed);

    ���������������������������������������������������������������������Slerp���������

    //������������
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3);
  • ������������

    scale���������������������������������������������������������������������������������������������������������

    //������������
    float speed = 5.0f;
    //���������������
    float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    //���������������
    float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    //������������
    transform.localScale += new Vector3(x, 0, z);
  • ������������

    ���������������������������������������Unity���������������Transform���������������������������������������������������������������X���������������������������������������������������������������������������������������������������������������������������

    上一篇:使用DoTweenPro插件实现场景漫游功能(摄像机按照指定轨迹移动)
    下一篇:Unity3D中实现控制Update方法中帧率执行方法(降低Update方法中代码执行频率)节约资源

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月04日 01时43分34秒