- Home /
Question by
$$anonymous$$ · Aug 15, 2019 at 10:35 AM ·
c#3dcamera rotate
Need help, preventing third person camera from rotating on z axis. [C#] [3D]
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour
{
public float horizontalSpeed = 2.0F;
public float verticalSpeed = 2.0F;
void LateUpdate()
{
float h = horizontalSpeed * Input.GetAxis("Mouse X");
float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(v, h, 0);
transform.rotation = new Vector3(transform.rotation.x, transform.rotation.y, 0);
}
}
Can I please get some help on what I'm doing wrong? The compiler says this: 'Cannot implicity convert type 'UnityEngine.Vector3' to 'UnityEngine.Quaternion.'' I hope that gives you guys a clue, I sure don't. This is my first project.
Comment
Best Answer
Answer by Namey5 · Aug 16, 2019 at 09:52 AM
'transform.rotation' is not a Vector3, but rather a Quaternion. You want to access the angles held within the rotation property, then cast those back to a quaternion and assign the rotation that way, i.e.
transform.rotation = Quaternion.Euler (new Vector3(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0));
Thanks! Works perfectly now. now all I need to do is figure out how to do camera-based motion direction