Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Player slows down the more thet look up or down.

Discussion in 'Scripting' started by whydowexist, Jun 13, 2020.

  1. whydowexist

    whydowexist

    Joined:
    Jun 13, 2020
    Posts:
    5
    Im making a FPS character and the more the player looks up or down, the slower the character gets, this doesnt affect moving from left to right.

    Code (CSharp):
    1.   var projectedForward = Vector3.ProjectOnPlane(eyeCamera.forward * 100f, Vector3.up);
    2.  
    3.         var projectedRight = Vector3.ProjectOnPlane(eyeCamera.right, Vector3.up);
    4.  
    5.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    6.  
    7.         if(isGrounded && velocity.y < 0)
    8.         {
    9.           velocity.y = -2f;
    10.         }
    11.  
    12.         float x = Input.GetAxis("Horizontal");
    13.         float z = Input.GetAxis("Vertical");
    14.  
    15.         Vector3 move = projectedRight * x + projectedForward * z;
    16.  
    17.         controller.Move(move * speed * Time.deltaTime);
    18.  
    19.         velocity.y += gravity * Time.deltaTime;
    20.  
    21.         controller.Move(velocity * Time.deltaTime);
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,113
    The length of your
    projectedForward
    vector changes with the camera angle, it needs to be normalized to be used as a direction vector.

    The same would happen to the
    projectedRight
    vector, if the camera was ever tilted left/right. Since it only ever tilts up/down or rotates around, the right vector will always stay parallel to the ground and its project length won't change.

    You want to normalize the
    move
    vector after creating it, this ensures the speed will stay constant whatever you do (you probably then need to decrease the speed to compensate the
    move
    vector now being one unit long instead of 100s).
     
  3. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    what is eyeCamera?

    if it is a transform you are checking for rotation, when you do eyecamera.forward its adding y into your vector which you dont want
     
    Last edited: Jun 13, 2020
  4. whydowexist

    whydowexist

    Joined:
    Jun 13, 2020
    Posts:
    5
    The problem is when I normalize it, you keep moving for a bit after not pressing any key and you can't walk diagonally.
    I did this:
    Code (CSharp):
    1. Vector3 move = Vector3.Normalize(projectedRight * x + projectedForward * z);
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,113
    Hm, do you still have the
    * 100f
    in the calculation of
    projectedForward
    ? This would mean that forward easily overpowers right if you press both.

    Maybe it's best to normalize both vectors before calculating
    move
    and then normalize it again. If you just remove the times 100, the diagonal will slightly change direction when up look up or down.

    As for continuing walking when you let go, did you check the sensitivity and gravity settings in the input manager? They control how quickly axes change when you press/release keys.
     
  6. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    the flawed part of your script is this field

    Code (CSharp):
    1. eyeCamera.forward
    you shouldn't use the forward of your camera's transform

    you need to replace the eyeCamera in this code with an object that is rotated the same as your camera horizontally but not vertically
     
  7. whydowexist

    whydowexist

    Joined:
    Jun 13, 2020
    Posts:
    5
    and how could i rotate an object only horizontally depending on the camera's rotation?
     
  8. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,113
    I don't agree. You're basically achieving the same result with two different approaches and there's nothing fundamentally flawed with the approach @greenjelly766 posted here. I think it's smart.