FloodAndBones
FloodAndBones

Reputation: 17

How do I make my third person shooter player look around?

I've genuinely gone through a million tutorials, but it doesn't work out. With this code, I either have to activate the last line or the second last line to get x rotation or y rotation respectively, but I obviously want them to work together.

void MouseLook()
{
    mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -60, 10);

    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    transform.Rotate(Vector3.up * mouseX);
}

The one always seems to cancel the other out. This is from a Brackeys tutorial. I had to tweak some stuff so it's applicable, but I obviously am breaking the code that way. Please help!

Upvotes: 0

Views: 161

Answers (1)

Sanjay Hadiya
Sanjay Hadiya

Reputation: 945

Try this Stuff

 public float mouseSensitivity = 10.0f;
 public Transform target;
 public float dstFromTarget = 2.0f;
 public float yaw;
 public float pitch;
 public Vector2 pitchMinMax = new Vector2(-50, 85);
 public float rotationSmoothTime = 0.02f;
 Vector3 rotationSmoothVelocity;
 Vector3 currentRotation;


 void LateUpdate()
 {
     MouseLook();
 }

 void MouseLook()
 {
     //Mouse Movement
     yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
     pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
     
     // Claemp
     pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);

     // Positioning
     transform.eulerAngles = currentRotation;
    
     
     // Smoothening
     currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);

     transform.localRotation = Quaternion.Euler(currentRotation.x,currentRotation.y,currentRotation.z);
 }

Upvotes: 0

Related Questions