0
votes

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.

1
Try this for camera restriction. That should work.Programmer
Doesn't work. transform.localEulerAngles screws up my camera and animations. edit: i've fixed it but still doesn't work. It stopped getting part of my mouse input.Giddy_Up
Thought you are moving the camera with some angle restriction. That code is for FPS camera and should be attached to a camera without animation.Programmer
Almost everything works now, but i still need to figure out how to rotate the camera into proper position. Everytime i launch a test my camera rotates 90 degrees around Z axis.Giddy_Up

1 Answers

0
votes

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); 

}