Search Unity

How do i create c# script that allows me to move when not grounded?

Discussion in 'Scripting' started by ConfusedEdBoy, Jul 6, 2015.

  1. ConfusedEdBoy

    ConfusedEdBoy

    Joined:
    Jul 6, 2015
    Posts:
    1
    The script doesnt allow me to move unless grounded
     

    Attached Files:

  2. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
  3. Rick Love

    Rick Love

    Joined:
    Oct 23, 2014
    Posts:
    76
    @ConfusedEdBoy The problem with your script is that you ignore all input whenever the max velocity is reached:

    So, if you are falling quickly, you won't be able to apply additional input:
    Code (CSharp):
    1. if (GetComponent<Rigidbody> ().velocity.magnitude < maxSpeed)
    An alternative approach would do this:
    • Always apply the input which does not affect your y position
    • Check the x and z magnitude of the velocity to see if their combined magnitude is greater than max speed (you can use their values to create a Vector2).
    • Scale the x and z back so it has maximum speed (you will need to use the normalized value of the Vector2 and scale it to max speed)