- Home /
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? :)
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;
}
}
Answer by almo · Jul 11, 2011 at 05:25 PM
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.
thank you for the raycast to improve my jump code :)...but unfortunately it doesn't work when I use "rigidbody.velocity += transform.right * curspeed;" either...:( so I don't think that's the problem :( plus isn't addrelativeforce and velocity two different things?
I'm not entirely sure. But I see nothing else wrong with the code. Try using addrelativeforce for the other movements, and see what happens. It may not give the motion you want, but if you can the jump and run, you know that's the issue.
yep it was cause I was using velocity...but now I accelerate out of control lol...I think I can somehow fix it though thanks :D
@deeredman1991 AddRelativeForce applies a force to the rigidbody relative to its orientation. During a physics step, all distributed forces (including automatic forces) acting on a single rigidbody are computed, and its velocity is changed based on the resultant net force. When someone applies a force to a rigidbody, he/she usually does so with the intention of changing the velocity. Applying a force to a rigidbody and setting its velocity to an absolute value are just 2 sides of the same coin for most intents and purposes.
In your case, you are setting the rigidbody's velocity to an absolute value after applying an impulse force to it. Distributed forces (Force$$anonymous$$ode.Force and Force$$anonymous$$ode.Acceleration) are only calculated during the next physics step so you can set the rigidbody's velocity prior to that and still fulfill your objective. However, impulse forces (Force$$anonymous$$ode.Impulse and Force$$anonymous$$ode.VelocityChange) are not "real forces" in the sense that they are immediately calculated into the rigidbody's velocity as soon as they are applied. Since you're setting the velocity to an absolute value after the impulse force is applied, any net change done by the impulse force to the velocity is nullified.
Further notes about your script:
Like almo mentioned, you're raycasting needlessly. Try to $$anonymous$$imize the raycasts whenever possible.
Set up virtual button axes ins$$anonymous$$d of accessing keycode events. Hardcoded input methods are quite sloppy and rarely necessary.
Proper use of the if-else conditional will save you a few hours of unwanted debugging.
Do NOT use AddRelativeForce and the Transform local space vectors (transform.right, transform.up, transform.forward) together. Going to the right of your right side is not the same as going to your right side. A slight rotation of your object will show you what I mean.
see Dremblur when I use the Input.GetAxis("Horizontal") my player likes to run right through colliders on occasion but maybe it's because the only way I know how to move the player after that is by using transform :(...the only way I saw to fix this was to use hardcoded input methods lol...I'm sure there is a better solution but I don't know what it is...however if you would be willing to shed some light on my predicament I would be eternally grateful :)
this is my code using the x axis
float wx = Input.GetAxis("Horizontal") Time.deltaTime curspeed;
transform.Translate(wx, 0, 0);
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Movement Jumping Help 0 Answers
CharacterController Jump from moving platform. 0 Answers
a better movement code C# 3 Answers
Player Falls Much Slower While Moving 2 Answers