N
N
Nikita Salnikov2020-02-18 11:16:59
C++ / C#
Nikita Salnikov, 2020-02-18 11:16:59

Why is the movement of the character following the movement of the mouse not the same?

Hello. I'm learning how to write code for character movement from the book "Unity in Action" by Joseph Hawking. I got to the code, which shows the movement of the character vertically. but for some reason it turns horizontally as expected, but vertically it turns out that the character leans to the left to the right, and not up and down. it should be?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour
{
    public enum RotationAxes
    {
        MouseXandY = 0,
        MouseX = 1,
        MouseY = 2

    }
    public RotationAxes axes = RotationAxes.MouseXandY;
    public float sesitivityHor = 9.0f;
    public float sensitivityVert = 9.0f;

    public float minimumVert = -45.0f;
    public float maximumVert = 45.0f;

    private float _rotationX = 0;

    // Start is called before the first frame update

    void Start()
    {
        Rigidbody body = GetComponent<Rigidbody>();
        if (body != null)
            body.freezeRotation = true;
        
    }

    // Update is called once per frame
    void Update()
    {
        if (axes == RotationAxes.MouseX)
    {
            transform.Rotate(0, Input.GetAxis("Mouse X") * sesitivityHor, 0, Space.World);
        }
        else if(axes == RotationAxes.MouseY)
        {
            
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
            
            float rotationY = transform.localEulerAngles.y;
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
        else
        {
            _rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);

            float delta = Input.GetAxis("Mouse X") * sesitivityHor;
            float rotationY = transform.localEulerAngles.y + delta;

            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }
    }
}

5e4b9d6632d53913377573.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanielMcRon, 2020-02-18
@nikitasalnikov

I had the same bug. On the camera you need to select the movement in y, on the character in x, so that the effect is normal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question