Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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)