- Home /
I cant look sideways with this code.
I don't get why I cant look sideways? I can look up and down fine though. the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseLook : MonoBehaviour {
public float mouseSensetivity = 100f;
public Transform playerbody;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensetivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensetivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerbody.Rotate(Vector3.up * mouseX);
}
}
What are you trying to do with this Script? I am trying to reproduce your issue but I'm unsure what your goal is here.
Answer by MenimalsEntertainment · May 30, 2020 at 05:41 AM
I'm not an expert in quaternions, so I may be totally wrong, but you could try using Euler angles instead. I have no idea if this will actually do anything, but you can read more here: https://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
Answer by Eno-Khaon · May 30, 2020 at 07:37 AM
You've been applying your current-frame's yaw (horizontal) rotation rather than the total value.
// This only applies your yaw for the current frame
playerbody.Rotate(Vector3.up * mouseX);
// You meant to use this:
playerbody.Rotate(Vector3.up * xRotation);
As an added note, however, Mouse input is provided and generally expected on a per-frame basis. You don't need to multiply it by Time.deltaTime, since the framerate will inherently influence the value provided as input.
Your answer
Follow this Question
Related Questions
[Solved :D] Camera auto turns 180° from its init position on Play (Animated GIF Included!) 1 Answer
How to make that the camera will not go through the walls? 1 Answer
How to Rotate FPS Camera independent of Parent Player Object while WallRunning ? 0 Answers
Camera not rotating 1 Answer
How Do i Prevent The First Person Camera From Turning 0 Answers