- Home /
Why is my camera rotating like crazy?
I'm following an online course on how to make a first person camera. The author of the course did it in a different way than I expected though: He converted the direction he wanted the player to go to world directions with "transform.TransformDirectory()". Then he created a script only for the x coordinates of the mouse called "Look X", attached to the player's object, then he created some sort of pivot (a child empy object attached to the player) and attached a "Look Y" script to it. The camera then was attached to the empty object instead of directly to the player (the order goes Player object -> LookY object -> Main Camera object). I wrote the scripts and set the scene just like he did, but instead of it working like it did for him, my camera just keeps moving around like crazy.
Below are the scripts I'm using:
Player script:
private CharacterController
_controller;
[SerializeField] private float _speed = 3.5f;
private float _gravity = 9.18f;
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
CalculateMovement();
}
void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, 0f,verticalInput);
Vector3 velocity = direction * _speed;
velocity.y -= _gravity;
velocity = transform.TransformDirection(velocity);
_controller.Move(velocity * Time.deltaTime);
}
LookX script:
SerializeField private float sensitivity = 1f;
void Start()
{
}
void Update()
{
float _mouseX = Input.GetAxis("Mouse X");
Vector3 newRotation = transform.localEulerAngles;
newRotation.y += _mouseX * sensitivity;
transform.localEulerAngles = newRotation;
}
LookY script:
void Update()
{
float mouseY = Input.GetAxis("Mouse Y");
Vector3 newRotation = transform.localEulerAngles;
newRotation.x -= mouseY;
transform.eulerAngles = newRotation;
}
Answer by unity_ek98vnTRplGj8Q · Jul 29, 2020 at 09:56 PM
Try changing transform.eulerAngles in your LookY script to transform.localEulerAngles.
Your answer
Follow this Question
Related Questions
Vertical axis not working properly 0 Answers
Making a Model move with the Mouse 1 Answer
Mobile input problem for first-person shooter 0 Answers
Mouse movement swings character around in circles 1 Answer
Mouse Look Help 2 Answers