P
P
password222020-12-04 17:10:16
Unity
password22, 2020-12-04 17:10:16

How to set the camera rotation along the Y axis with the character, and along the X axis without?

Good afternoon! I can't figure out how to make the camera look up without rotating the character, if the camera rotates along the y-axis along with the character. Itself in unity not so long ago, it is still difficult to understand and understand some things where there is some kind of mathematics.

This is the script of the character itself, I use two joysticks - one to run, the second to rotate the camera

public class Player : MonoBehaviour
{
    public SimpleTouchController leftController;
    private float movementSpeed = 7.0f;
    private float gravity = 20.0f;

    CharacterController character;
    Transform cameraTransform;
    Vector3 move = Vector3.zero;


    private void Start()
    {
        character = GetComponent<CharacterController>();
        cameraTransform = Camera.main.transform;
    }


    void Update()
    {
        float horizontal = leftController.GetTouchPosition.x;
        float vertical = leftController.GetTouchPosition.y;

        if(character.isGrounded)
        {
            move = new Vector3(horizontal, 0.0f, vertical);
            move = cameraTransform.TransformDirection(move);
        }

        move.y -= gravity * Time.deltaTime;
        character.Move(move * movementSpeed * Time.deltaTime);
    }

}


This is the camera script
public class Camera : MonoBehaviour
{
    public GameObject target; // персонаж
    public SimpleTouchController rightController;

    private float rotateSpeed = 5;
    private Vector3 offset;


    void Start()
    {
        offset = target.transform.position - transform.position;
    }

    void LateUpdate()
    {
        float horizontal = rightController.GetTouchPosition.x;
        float vertical = rightController.GetTouchPosition.y;

        target.transform.Rotate(0, horizontal, 0);

        float desiredAngle = target.transform.eulerAngles.y;    //берем вращение игрока в мировом пространстве
        Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0); //поворачиваем на y градусов вокруг оси y
        transform.position = target.transform.position - (rotation * offset);  

        transform.LookAt(target.transform);
    }
}


I re-read and revised the entire Internet, but I still can’t figure out quaternions, angles, and so on ..

I would be glad for any help! Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question