Help with Walker Bros Unity Course 'Lab 4' Tutorial

So ever since Tutorial 4 of this set here: http://walkerboystudio.com/html/unity_course_lab_4.html

My Mario Sprite has been flying straight up with no stop, when I click the jump button (Space Bar) instead of going up and comming down. Though the walkJump and the runJump isn’t working (flying straight up) the crouch jump and just the Jump with no movement is working for some strange reason. I am really stuck at the moment and am questioning what I have done wrong. Is it possible that anyone could help me? Here is the script:

var walkSpeed : float = 1.5; // speed of the standerd walk
var runSpeed : float = 2.0; // speed of the run
var fallSpeed : float = 2.0; // speed of falling down
var walkJump : float = 0.1; // jump height from walk
var runJump : float = 0.0; // jump height from run
var crouchJump : float = 10.0; // jump height from crouching
var gravity : float = 20.0; // force applied on character
var startPos : float = 0.0; // location for start position

var moveDirection : int = 0; // Direction that player faces start

private var velocity : Vector3 = Vector3.zero; // Speed of player and direction

function Update ()
{
var aniPlay = GetComponent ( “aniSprite”);
//aniPlay.aniSprite ( 16, 16, 0, 1, 16, 12);
var controller : CharacterController = GetComponent( CharacterController );

if ( controller.isGrounded )
{
velocity = Vector3 ( Input.GetAxis ( “Horizontal” ), 0, 0 );
}
if ( velocity.x == 0 moveDirection == 1 ) //idle right
{
aniPlay.aniSprite ( 16, 16, 0, 0, 16, 12); // animation to call sprite sheet
}
if ( velocity.x == 0 moveDirection == 0 ) // idle left
{
aniPlay.aniSprite ( 16, 16, 0, 1, 16, 12); // animation to call sprite sheet
}
if ( velocity.x < 0 ) // walk left
{
velocity *= walkSpeed; // move left based on walk speed
aniPlay.aniSprite ( 16, 16, 0, 3, 10, 15 );
}
if ( velocity.x > 0 ) // walk right
{
velocity *= walkSpeed; // move right based on walk speed
aniPlay.aniSprite ( 16, 16, 0, 2, 10, 15 );
}
if ( velocity.x < 0 Input.GetButton ( “Fire1” ) ) // run left
{
velocity *= runSpeed; // move/run left based on run speed
aniPlay.aniSprite ( 16, 16, 0, 5, 16, 24 );
}
if ( velocity.x > 0 Input.GetButton ( “Fire1” ) ) // run right
{
velocity *= runSpeed; // move/run right based on run speed
aniPlay.aniSprite ( 16, 16, 0, 4, 16, 24 );
}
if ( velocity.x == 0 Input.GetAxis ( “Vertical” ) < 0 )
{
if ( moveDirection == 0 ) // idle left

aniPlay.aniSprite ( 16, 16, 0, 9, 16, 24 );

if ( moveDirection == 1 ) // idle right

aniPlay.aniSprite ( 16, 16, 0, 8, 16, 24 );
}
if ( Input.GetButtonDown ( “Jump” ) ( !Input.GetButton ( “Fire1” ) || Input.GetButton ( “Fire1” ) velocity.x == 0 ) Input.GetAxis ( “Vertical” ) >= 0 )
{
velocity.y = walkJump;
}
if (Input.GetButtonDown ( “Jump” ) Input.GetButton ( “Fire1” ) velocity.x != 0 )
{
velocity.y = runJump;
}
if ( Input.GetButtonDown ( “Jump” ) velocity.x == 0 Input.GetAxis ( “Vertical” ) < 0 )
{
velocity.y = crouchJump;
}
if ( !controller.isGrounded )
{
velocity.x = Input.GetAxis ( “Horizontal”);
velocity.x *= walkSpeed;
}
if ( velocity.x < 0 ) //get last move direction- left
{
moveDirection = 0; // set moveDirection to left
}
if ( velocity.x > 0 ) //get last move direction to right
{
moveDirection = 1; // set moveDirection to right
}

velocity.y -= gravity * Time.deltaTime; // apply gravity
controller.Move ( velocity * Time.deltaTime ); // move the controller
}

I really don’t know why the jump feature isn’t working proparly. Can someone please take the time to answer my question!

Thanks :slight_smile:

Anyone?

I am a lot further along that you are but check my code against yours. Mine is working.

var walkSpeed					: float		= 1.5;								// speed of standard walk
var runSpeed					: float 	= 2.0;								// speed of run
var fallSpeed					: float		= 2.0;								// Speed of falling down
var walkJump					: float 	= 6.0;								// jump height from walk
var runJump						: float 	= 9.0;								// jump height from run
var crouchJump					: float 	= 10.0;								// jump height from crouch
var gravity						: float 	= 20.0;								// force applied on char
var startPos					: float 	= 0.0;								// location for start posistion

var moveDirection				: int		= 1;								// direction player is facing // left = 0 // right = 1

var particleJump				: Transform;									// particle for feet hitting the ground

var soundJump					: AudioClip;									// audio clip for jump
var soundCrouchJump				: AudioClip;									// audio clip for crouch jump

private var soundRate			: float		= 0.0;								// variable holds current time + delay ammount
private var soundDelay			: float		= 1.0;								// amount to delay for playing sound

private var velocity 			: Vector3 	= Vector3.zero;						// speed of player
private var jumpEnable 			: boolean 	= false;							// toggle for jump standard
private var runJumpEnable 		: boolean 	= false;							// toggle for run jump
private var crouchJumpEnable	: boolean 	= false;							// toggle for crouch jump
private var afterHitForceDown	: float		= 1.0;								// force player down if head hits anything

function PlaySound ( soundName, soundDelay )
{
	if ( !audio.isPlaying  Time.time > soundRate )
	{
		soundRate = Time.time + soundDelay;
		audio.clip = soundName;
		audio.Play();
		yield WaitForSeconds ( audio.clip.length );
	}
}

function Update()
{
	var particlePlacement		: Vector3 = Vector3 ( transform.position.x, transform.position.y - .5, transform.position.z ); // set particle at base of sprite
	var aniPlay = GetComponent ( "aniSprite" );									// set aniPlay to aniSprite.js
	var controller : CharacterController = GetComponent( CharacterController ); // Set controller to character
	
	if ( controller.isGrounded )												// if character is on the ground
	{
		jumpEnable = false;														// reset jumps
		runJumpEnable = false;													// reset jumps
		crouchJumpEnable = false;												// reset jumps
		
		velocity = Vector3 ( Input.GetAxis ( "Horizontal" ), 0, 0 );
		
		if ( velocity.x == 0  moveDirection == 1 )							// idle right
		{
			aniPlay.aniSprite (16, 16, 0, 0, 16, 12);							// animation call to sprite sheet
		}
		
		if ( velocity.x == 0  moveDirection == 0 )							// idle left
		{
			aniPlay.aniSprite (16, 16, 0, 1, 16, 12);							// animation call to sprite sheet
		}
		
		if ( velocity.x < 0 )													// walk left
		{
			velocity *= walkSpeed;												// move left based on walk speed
			aniPlay.aniSprite (16, 16, 0, 3, 10, 15);							// animation call to sprite sheet
		}
		
		if ( velocity.x > 0 )													// walk right
		{
			velocity *= walkSpeed;												// move right based on walk speed
			aniPlay.aniSprite (16, 16, 0, 2, 10, 15);							// animation call to sprite sheet
		}
		
		if ( velocity.x < 0  Input.GetButton ( "Fire1" ) )					// run left
		{
			velocity *= runSpeed;												// move/run left based on run speed
			aniPlay.aniSprite (16, 16, 0, 5, 16, 24);							// animation call to sprite sheet
		}
		
		if ( velocity.x > 0  Input.GetButton ( "Fire1" ) )					// run right
		{
			velocity *= runSpeed;												// mov/run right based on run speed
			aniPlay.aniSprite (16, 16, 0, 4, 16, 24);							// animation call to sprite sheet
		}
		
		if ( velocity.x == 0  Input.GetAxis ( "Vertical" ) < 0 )
		{
			if ( moveDirection == 0)											// crouch left
			{
				velocity.x = 0;													// keep player from moving
				aniPlay.aniSprite (16, 16, 0, 9, 16, 24);						// animation call to sprite sheet
			}
			
			if ( moveDirection == 1)											// crouch right
			{
				velocity.x = 0;													// keep player from moving
				aniPlay.aniSprite (16, 16, 0, 8, 16, 24);						// animation call to sprite sheet
			}
			
		}
			
		if ( velocity.x == 0  Input.GetAxis ( "Vertical" ) > 0 )
		{
			aniPlay.aniSprite (16, 16, 0, 9, 16, 24);							// animation call to sprite sheet
		}
			
		if ( Input.GetButtonDown ( "Jump" )  ( !Input.GetButton ( "Fire1" ) || Input.GetButton ( "Fire1" )  velocity.x == 0 )  Input.GetAxis ( "Vertical" ) >= 0 ) // check if jump button is pressed but not the fire1 or fire1 button is pressed and the player is not moving and down is not pressed
		{
			velocity.y = walkJump;												// setting the y value walkJump
			Instantiate ( particleJump, particlePlacement, transform.rotation );
			PlaySound ( soundJump, 0 );											// play sound for jump
			jumpEnable = true;													// enable jump standard
		}
		if ( Input.GetButtonDown ( "Jump" )  Input.GetButton ( "Fire1")  velocity.x != 0 ) // check if jump button and fire1 buttons are pressed and player is not standing still
		{
			velocity.y = runJump + 5;											// setting the y value runJump
			Instantiate ( particleJump, particlePlacement, transform.rotation );
			PlaySound ( soundJump, 0 );											// play sound for jump
			runJumpEnable = true;												// enable runJump
		}
		if ( Input.GetButtonDown ( "Jump" )  velocity.x == 0  Input.GetAxis ( "Vertical") < 0 )	// check is jump button is pressed and player is not moving and down is pressed
		{
			velocity.y = crouchJump;											// setting the y value crouchJump
			Instantiate ( particleJump, particlePlacement, transform.rotation );
			PlaySound ( soundCrouchJump, 0 );										// play sound for crouch jump
			crouchJumpEnable = true;											// enable crouchJump
		}
		
	}
	if ( !controller.isGrounded )												// if player is in the air
	{
		velocity.x = Input.GetAxis ( "Horizontal" );							// set the horizontal speed from input
		
		if ( Input.GetButtonUp ( "Jump" ) )										// jump control height
		{
			velocity.y = velocity.y - fallSpeed;								// subtract current height from 1 if jump button is up
		}
		if ( moveDirection == 0 )												// facing left
		{
			if ( jumpEnable)													// jump left standard
			{
				velocity.x *= walkSpeed;										// walk left speed * current move speed
				aniPlay.aniSprite (16, 16, 11, 3, 4, 12);						// animation call to sprite sheet
			}
			if ( runJumpEnable)													// run left jump
			{
				velocity.x *= runSpeed;											// run left jump speed * current move speed
				aniPlay.aniSprite (16, 16, 11, 3, 4, 12);						// animation call to sprite sheet
			}
			if ( crouchJumpEnable)												// crouch left jump
			{
				velocity.x *= walkSpeed;
				aniPlay.aniSprite (16, 16, 12, 11, 4, 12);						// animation call to sprite sheet
			}
		}
		if ( moveDirection == 1 )												// facing right 
		{
			if ( jumpEnable)													// jump right standard
			{
				velocity.x *= walkSpeed;										// walk right speed * current move speed
				aniPlay.aniSprite (16, 16, 11, 2, 4, 12);						// animation call to sprite sheet
			}
			if ( runJumpEnable)													// run right jump
			{
				velocity.x *= runSpeed;											// run right jump speed * current move speed
				aniPlay.aniSprite (16, 16, 11, 2, 4, 12);						// animation call to sprite sheet
			}
			if ( crouchJumpEnable)												// crouch right jump
			{
				velocity.x *= walkSpeed;
				aniPlay.aniSprite (16, 16, 12, 10, 4, 12);						// animation call to sprite sheet
			}
		}
	}	
	
	if ( velocity.x < 0 )														// get last move direction - left
	{
		moveDirection = 0;														// set moveDirection to left
	}
	
	if ( velocity.x > 0 )														// get last move direction - right
	{
		moveDirection = 1;														// set moveDirection to right
	}
	
	if ( controller.collisionFlags == CollisionFlags.Above )
	{
		velocity.y = 0;															// set velocity on y to 0, stops upward movement
		velocity.y -= afterHitForceDown;										// apply 'force' downward sp player does not hang in air
	}
	
	
	velocity.y -= gravity * Time.deltaTime;										// apply gravity
	controller.Move ( velocity * Time.deltaTime );								// move the controller
}