A
A
Artyom2020-01-26 10:50:07
C++ / C#
Artyom, 2020-01-26 10:50:07

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

1 answer(s)
A
Artyom, 2020-01-26
Maidurov @sylniyduxom

The problem was solved by changing the main object. To solve this problem, you need not to attach the camera to the object, but the object to the camera. At the same time, both Rigibody and Collider need to be weighed on the camera

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question