-1
\$\begingroup\$

I don't understand how this works. How does this code go about manipulating the player's rigidbody velocity? Can you explain it piece by piece?

//Movement
public float speed;
public float jump;
float moveVelocity;

//Grounded Vars
bool grounded = true;

void Update ()
{
  //Jumping
  if ( Input.GetKeyDown (KeyCode.Space) 
    || Input.GetKeyDown (KeyCode.UpArrow) 
    || Input.GetKeyDown (KeyCode.Z) 
    || Input.GetKeyDown (KeyCode.W))
  {
    if(grounded)
    {
      GetComponent<Rigidbody2D>().velocity = new Vector2(
        GetComponent<Rigidbody2D>().velocity.x, 
        jump);
    }
  }

  moveVelocity = 0;

  //Left Right Movement
  if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
  {
    moveVelocity = -speed;
  }
  if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
  {
    moveVelocity = speed;
  }

  GetComponent<Rigidbody2D>().velocity = new Vector2(
    moveVelocity, 
    GetComponent<Rigidbody2D>().velocity.y);
}
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

4 member variables that persist between calls to Update:

//Movement
public float speed;
public float jump;
float moveVelocity;

//Grounded Vars
bool grounded = true;

In Unity, Update() is called on a MonoBehaviour once per frame:

void Update ()
{

This detects the four different jump buttons:

    //Jumping
    if ( Input.GetKeyDown (KeyCode.Space) 
        || Input.GetKeyDown (KeyCode.UpArrow) 
        || Input.GetKeyDown (KeyCode.Z) 
        || Input.GetKeyDown (KeyCode.W))
    {

If a jump button is pressed, this checks if the player is on the ground:

        if(grounded) {

Gets the rigid body attached to this script's GameObject and sets its velocity property using its current speed in direction x and whatever the speed jump was set to in direction y:

            GetComponent<Rigidbody2D>().velocity = new Vector2(
                GetComponent<Rigidbody2D>().velocity.x, 
                jump);
        }
    }

Resets the a member variable and detects left and right inputs to change the speed in direction x:

    moveVelocity = 0;

    //Left Right Movement
    if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) {
        moveVelocity = -speed;
    }
    if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) {
        moveVelocity = speed;
    }

    GetComponent<Rigidbody2D>().velocity = new Vector2(
        moveVelocity, 
        GetComponent<Rigidbody2D>().velocity.y);
}

The most notable problem with this script is that grounded doesn't ever seem to be set so I'm guessing you've omitted that part. Also, calling GetComponent<> is pretty expensive when done like this. It would make more sense to save the Rigidbody2D as a member variable from Start():

private Rigidbody2d body;

void Start() {
    body = GetComponent<Rigidbody2D>();
}

Then you can just use body.velocity.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .