Forum rules - please read before posting.

Limiting the X rotation on FPS camera

edited April 2015 in Technical Q&A
This is just a personal request (sorry), as I'm making a few tests for a particular type of movement.
Basically, I wanted to add a limitation to the rotation on the X, just like there is one on the height, so that effectively you can't turn yourself for 360°. We are talking about the FPSCamera script.

I already edited the Editor part and started messing with the code in the FPSCamera script,
I added

public float rotationX = 0f;
    public float minX = -60F;
    public float maxX = 60F;

and then

    private void FixedUpdate ()
    {
        rotationY = Mathf.Clamp (rotationY, minY, maxY);
        transform.localEulerAngles = new Vector3 (rotationY, 0, 0);
        rotationX = Mathf.Clamp (rotationX, minX, maxX);
        transform.localEulerAngles = new Vector3 (rotationX, 0, 0);
    }

    public void SetRotationY (float tilt)
    {
        rotationY = tilt;
    }

    public void SetRotationX (float tilt)
    {
        rotationX = tilt;
    }

However, it doesn't work, or rather, it is still affecting the Y, and I know why, it's because these variables aren't directly referencing to the X axis itself actually. Unfortunately, I couldn't find in the script the part to edit so that this works correctly, and that's probably because I'm an embarassing coder. Could you help me Chris if, as I hope, it's something quick?
Apologies

David

Comments

  • You're setting the localEulerAngles twice in FixedUpdate, which means they're not being set properly.

    You want something more along the lines of:

    private void FixedUpdate ()
    {
        rotationY = Mathf.Clamp (rotationY, minY, maxY);
        rotationX = Mathf.Clamp (rotationX, minX, maxX);
        transform.localEulerAngles = new Vector3 (rotationY, rotationX, 0);
    }


  • thanks! I understand now.
    However, despite no console errors, it seems to not work, it is basically as it works normally, but it still lets me turn completely. Weird.
    Ah well, please don't bother with this anyway, I just wanted to do a test but I'm thinking about discarding it.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.