Answer the question
In order to leave comments, you need to log in
How to write a camera rotation script so that an object rotates with the camera in Unity 3D?
I am writing a script for turning the camera on Unity, but I also need the object to rotate. I tried to rotate using Quaternion.Euler, but the object rotates a little slower, so there are some problems. Here is the code I found:
public class movemouse2 : MonoBehaviour
{
public Transform head;
public Transform body;
public float sensitivity = 5f; // чувствительность мыши
public float headMinY = -40f; // ограничение угла для головы
public float headMaxY = 40f;
private float rotationY;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float rotationX = head.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
rotationY += Input.GetAxis("Mouse Y") * sensitivity;
rotationY = Mathf.Clamp(rotationY, headMinY, headMaxY);
head.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question