Saturday, March 25, 2017

Making our character jump.

Today comes the time when our inquisitor will jump for the first time. So let's define our input.
If we touch the screen with 2nd finger our character will jump.

First let's add Rigidbody 2D component from our inspector window.

Now open our previously created movement script add add a new public Rigidbody2D variable:

public Rigidbody2D rb;

Add it to our class but don't put it to any of functions inside it.

Now assign our Rigidbody2D component created in "Inspector" window to previously created variable. Put our code inside Start function.

 rb = GetComponent<Rigidbody2D>();

Now create a force that will push our sprite up when we touch the screen with the 2nd finger:
if (Input.GetTouch(1).phase == TouchPhase.Began)
            rb.AddForce(new Vector3(0, 10, 0), ForceMode2D.Impulse);

Put it inside Update function to check every frame if the screen has been touched.
GetTouch(1) means the 2nd finger because we are starting counting from 0 and if 2nd finger touched (Began) the screen this if statement is equal to truth.

No comments:

Post a Comment