Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question First Person - Limiting player (camera) rotation

Discussion in 'Editor & General Support' started by Hi_ImTemmy, Sep 28, 2022.

  1. Hi_ImTemmy

    Hi_ImTemmy

    Joined:
    Jul 8, 2015
    Posts:
    174
    Hey folks,

    I'm working on a first person character controller, and what I want to do is have the ability to restrict the player's horizontal rotation on some occasions.

    At the moment, as the mouse is moved, the player is rotated like this:

    transform.Rotate(Vector3.up * currentMouseDelta.x);


    This works great when there is no restriction.

    Because that takes a Vector3, what I've tried to do is set two limit vectors that represent any constraint. For example:


    What we can do then is check the player's angle as a float to see if the player has reached a limit and then do something like

    transform.forward = outwardLimitVector;


    This works, but only if the player is moving the mouse slowly. If they supply a large delta (move the mouse very fast) then they jump over the limit. The checks then seem to break down because numbers suddenly change from 180 to -180 which makes any greater than, or less than, check tricky to do.

    Any advice about how to better do this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,364
    If the player inputs a massive input, process it in a loop, iterating it one piece at a time.

    Current rotation = 10

    Proposed next-frame rotation = 300

    "Okay, too much angle..."

    Therefore iterate in 10 degree steps seeing if it clips the limit, then stop it.
     
  3. Hi_ImTemmy

    Hi_ImTemmy

    Joined:
    Jul 8, 2015
    Posts:
    174
    Thanks as always @Kurt-Dekker. Will give it some thought!