首页 > 解决方案 > Unity3D:需要帮助旋转角色而不涉及位置

问题描述

使用 Unity3D 2018.2.8

我试图在不涉及/改变位置的情况下旋转我的角色(以第一人称)。

相机附在角色身上。因此,在移动角色时,摄像机也随之移动。我想做一个拖动类型的触摸来移动对象旋转 360 度。

不确定我是否使用此代码以正确的方式前进

另外,我应该Quaternion用来控制旋转吗?

是相机targetToLookAt所在物体的变换

public Camera cameraSelf;
private Transform cameraTransform;
private Vector2 cameraRotation;

void FixedUpdate() {
  //  Camera.transform to be the same as camera targets body
  cameraSelf.transform.position = targetToLookAt.GetChild(0).GetChild(0).position;
  cameraSelf.transform.rotation = targetToLookAt.GetChild(0).GetChild(0).rotation;

  //    Moving camera with Mouse
  float pointer_x = Input.GetAxis("Mouse X");
  float pointer_y = Input.GetAxis("Mouse Y");
  //  Using Touch
  pointer_x = Input.touches[0].deltaPosition.x;
  pointer_y = Input.touches[0].deltaPosition.y;

  //  Mouse Movement
  cameraRotation.x += pointer_x * moveSensitivity * Time.deltaTime;
  cameraRotation.y -= pointer_y * moveSensitivity * Time.deltaTime;

  //  Set Rotation of Object
  targetToLookAt.position = cameraRotation;
}

标签: unity3dquaternions

解决方案


推荐阅读