- Home /
Question by
prashinmore · Jun 04, 2020 at 03:46 PM ·
c#scripting problemcharactercontrollermouselook
player isn't rotating along y-axis
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseLook : MonoBehaviour { public float mouseSpeed = 100f; public Transform playerBody; float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSpeed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSpeed * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
so the above is my mouselook script code but the playerBody.Rotate(Vector3.up * mouseX); part won't work and the character isn't rotating sideways at all the rest of the scripts are functioning and there are no errors it also rotates up and down but not sideways
Comment
Best Answer
Answer by tadadosi · Jun 07, 2020 at 11:33 AM
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float rotationRate = 20f;
public float mouseSpeed = 100f;
public Transform playerBody;
private float xRotation = 0;
private float yRotation = 0;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSpeed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSpeed * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
// If you use mouseX directly without adding or subtracting like this
// yRotation -= mouseX; it will simply be an input that goes from negative
// to positive while you are moving it, but will inmediatly go back to 0
// a soon as you stop moving the mouse. This means that the amount of
// rotation you set while using the mouse will simply go back to 0,
// hence no rotation at all.
yRotation -= mouseX;
transform.Rotate(Vector3.up * yRotation);
}
}