<aside> <img src="/icons/stars_yellow.svg" alt="/icons/stars_yellow.svg" width="40px" />

This assignment is a continuation of the 2d platformer we started making in class. In addition to having a player and enemy that can move horizontally, we’ll be adding a jump button, platforms, and additional enemies.

</aside>

Jumping

The “jump” method follows the same general pattern as horizontal movement. First I added a jumpForce variable to be able to change how high the player can independently of the speed variable.

To make the player object actually jump, I played with using both transform.Translate (which is identical to how I did horizontal movement), and rigidBody.AddForce (which makes use of Unity’s physics system).

public float jumpForce = 2.0f;

. . .

void Update()
{
		if (Input.GetKey(jump))
		{
		  // transform.Translate(Vector2.up * jumpForce * Time.deltaTime);
		  spidermanRB.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
		}
}

And it works! Hitting the spacebar now launches the payer into the air, just like the comics!

I ran into a couple of… interesting bugs as I was trying to dial in jumpForce:

The bugs are hilarious tho.

The bugs are hilarious tho.

Adding enemies

There’s probably a muuuch better way to do this, but I multiplied Snorlax by copy-pasting his prefab and making sure the clones were pointing to the same EnemyMovement.cs script.

Simple but effective

Simple but effective