Can someone review a short script and tell me where I've gone wrong? [JavaScript]

I have began coding a controller for a 2D game and I am having a few issues. Could anyone help me out please! alt text

Code

#pragma strict

	var moveUp : KeyCode;
	var moveDown : KeyCode;
	var moveRight : KeyCode;
	var moveLeft : KeyCode;
	
	var speed : float = 10;
	var jumpHeight = 8;
	
function Update () 
{
	if (Input.GetKey(moveUp) && isFalling == false)
	{
		rigidbody2D.velocity.y = jumpHeight;
	}
	isFalling = true;
	
	else if (Input.GetKey(moveDown))
	{
		rigidbody2D.velocity.y = speed *-1;
	}
	else if (Input.GetKey(moveRight))
	{
		rigidbody2D.velocity.x = speed;
	}
	else if (Input.GetKey(moveLeft))
	{
		rigidbody2D.velocity.x = speed *-1;
	}
	else 
	{
		rigidbody2D.velocity.y = 0;
		rigidbody2D.velocity.x = 0;
	}
}

you should set isFalling to false when you land and set it to false when you jump. function OnCollisonStay2D( collision2D :Collision2D) { if(collision2D.comparetag("Ground")) isFalling = false; }

Hi, I am trying to implement this within my code right now. The problem is I've never seen these pieces of code before and I'm not sure where they go! Also when you jump wouldn't isFalling be true? Could you show me how the code should be please?

"{" is a bracket

Thanks. I was just wondering if it had a different name within the coding world!

Curly brace/bracket :D

1 Answer

1

check the script reference. It is one of the biggest assets of the Unity engine, and it quickly improves your understanding on basic scripting stuff