Why can't I jump and run at the same time? c#

Hey guys I wrote a c# script for running and jumping…unfortunately it doesn’t allow me to run and jump at the same time…I can run and I can jump but not both at the same time anyone mind helping me out on this one? :slight_smile:

int isstuned = 0;


    //if the player isn't stunned
    		if(isstuned == 0)
    		{
    			//if space is pressed and the ray is reading a collision is down or if up arrow is pressed and the ray is reading a collision of down
    			if(Input.GetKeyDown("space") && Physics.Raycast(transform.position, -transform.up,1) || Input.GetKeyDown(KeyCode.UpArrow) && Physics.Raycast(transform.position, -transform.up,1))
    			{
    				//make the player transform up by the speed of jumpforce using an impulse type force
    				rigidbody.AddRelativeForce(transform.up * jumpforce, ForceMode.Impulse);
    			}
    			//if a or left arrow is pressed
    			if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
    			{
    				//push left
    				rigidbody.velocity = -transform.right * curspeed;
    			}
    			//if d or right arrow is pressed
    			if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
    			{
    				//push right
    				rigidbody.velocity = transform.right * curspeed;
    			}
    		}

Maybe it’s because your

rigidbody.velocity = transform.right * curspeed;

Sets the speed. It doesn’t add to it. I’m guessing that whatever you’re doing in the jump code gets overridden by the run code.

Also, not related to this problem, you want this

if((Input.GetKeyDown("space") || Input.GetKeyDown(KeyCode.UpArrow)) && Physics.Raycast(transform.position, -transform.up,1))

not this

 if(Input.GetKeyDown("space") && Physics.Raycast(transform.position, -transform.up,1) || Input.GetKeyDown(KeyCode.UpArrow) && Physics.Raycast(transform.position, -transform.up,1))

Your code does a redundant Raycast, and these are expensive.