围绕角色旋转相机(上下)Unity3D
Rotate camera around the character (up and down) Unity3D
我有一个以第三人称视角移动相机的脚本,我让相机随着角色左右转动,但我无法让相机上下转动角色,相机自动开机!
public float mouseSensitivity;
public bool invertMouse;
public bool autoLockCursor;
public Transform cam;
public Transform RotationX;
float x;
void Awake () {
Cursor.lockState = (autoLockCursor)?CursorLockMode.Locked:CursorLockMode.None;
}
void Update () {
//Rotates the player left and right along with the camera
this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
this.gameObject.transform.localEulerAngles = new Vector3(0 , this.gameObject.transform.localEulerAngles.y, 0);
//rotates an example object to get its transformer and uses it to rotate the camera
RotationX.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
RotationX.gameObject.transform.localEulerAngles = new Vector3(RotationX.gameObject.transform.localEulerAngles.x, RotationX.gameObject.transform.localEulerAngles.y, 0);
x = RotationX.gameObject.transform.localEulerAngles.x;
cam.gameObject.transform.localEulerAngles = new Vector3(x , 0, 0);
if (Cursor.lockState == CursorLockMode.None && Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
}
else if (Cursor.lockState == CursorLockMode.Locked && Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
我研究了 Unity 文档并找到了解决方案,创建一个空 object 并将相机添加为他的 child,在轴上移动后我只想要这个 object!
this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
this.gameObject.transform.localEulerAngles = new Vector3(this.gameObject.transform.localEulerAngles.x ,0 , 0);
我有一个以第三人称视角移动相机的脚本,我让相机随着角色左右转动,但我无法让相机上下转动角色,相机自动开机!
public float mouseSensitivity;
public bool invertMouse;
public bool autoLockCursor;
public Transform cam;
public Transform RotationX;
float x;
void Awake () {
Cursor.lockState = (autoLockCursor)?CursorLockMode.Locked:CursorLockMode.None;
}
void Update () {
//Rotates the player left and right along with the camera
this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
this.gameObject.transform.localEulerAngles = new Vector3(0 , this.gameObject.transform.localEulerAngles.y, 0);
//rotates an example object to get its transformer and uses it to rotate the camera
RotationX.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
RotationX.gameObject.transform.localEulerAngles = new Vector3(RotationX.gameObject.transform.localEulerAngles.x, RotationX.gameObject.transform.localEulerAngles.y, 0);
x = RotationX.gameObject.transform.localEulerAngles.x;
cam.gameObject.transform.localEulerAngles = new Vector3(x , 0, 0);
if (Cursor.lockState == CursorLockMode.None && Input.GetMouseButtonDown(0))
{
Cursor.lockState = CursorLockMode.Locked;
}
else if (Cursor.lockState == CursorLockMode.Locked && Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
我研究了 Unity 文档并找到了解决方案,创建一个空 object 并将相机添加为他的 child,在轴上移动后我只想要这个 object!
this.gameObject.transform.Rotate(Input.GetAxis("Mouse Y") * mouseSensitivity * ((invertMouse) ? 1 : -1), Input.GetAxis("Mouse X") * mouseSensitivity * ((invertMouse) ? -1 : 1), 0);
this.gameObject.transform.localEulerAngles = new Vector3(this.gameObject.transform.localEulerAngles.x ,0 , 0);