Giddy_Up
Giddy_Up

Reputation: 11

Unity head Y axis rotation clamp

I'm trying to make a FPP mode in unity where you can see your actual body. I've created a model, rigged everything. My head will rotate to the camera, but i don't want the player to be able to rotate around his body. I've already clamped rotation on x axis, but have problems with clamping around y axis.

void Update () {
currentBodyRotation = body.GetComponent<Transform> ().rotation.eulerAngles.y;

    yaw += Input.GetAxis ("Mouse X") * mouseSensitivity;


    yawMin = currentBodyRotation - 90f;
    yawMax = currentBodyRotation + 90f;

    yaw =  Mathf.Clamp (yaw, yawMin, yawMax);



    pitch -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
    pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);


    currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    transform.rotation = Quaternion.Euler (currentRotation);

}

I think rotation has limits when in 0 and 360 angles. My code works perfectly until body hits 360 degrees. When it does though my camera will jerk and just "bounce" off from invisible wall back to the side where it came from.

Upvotes: 0

Views: 1009

Answers (1)

Giddy_Up
Giddy_Up

Reputation: 11

Ok. I've figured it out. Here's code if someone has similar problem, maybe it will work for them too.

void Update () {

    yaw += Input.GetAxis ("Mouse Y") * mouseHorizontalSensitivity;


    yaw =  Mathf.Clamp (yaw, -30f, 80f);  // It's pitch but my code works weird



    pitch -= Input.GetAxis ("Mouse X") * mouseVerticalSensitivity;
    pitch = Mathf.Clamp (pitch, -90f, 90f);  // it's yaw


    currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (-pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    transform.localEulerAngles = currentRotation;

// my mouse input wouldn't go down which caused the model to rotate indeffinitely so i added smoothing to 0 for rotating around Y axis which here is labeled as pitch
    if (!Input.GetKey (KeyCode.LeftAlt))
        pitch = Mathf.SmoothDamp(pitch, 0f, ref rotationSmoothVelocity2, rotationSmoothTime); 

}

Upvotes: 0

Related Questions