- Home /
C# 2D InfiniteRunner run with same speed even when going up
I'm making a InfiniteRunner that you have to jump over obstacles and stuff. Sometimes there is a jump on the way. Thats where the problem starts.
Okay so what I'm trying to do is when my maincharacter goes forward on the jump that it keeps its speed, so it doesn't slow down. But because of the gravity or something it isn't, it goes half way then it falls back down.
How can I fix this? Turning off the gravity isn't a very good option because the maincharacter won't find the floor ever again if it jumps.
Maybe there's a way that the force is always constant?
I can't make sense of your question. Your problem is that it falls back down, but you still want it to find the floor? How is it supposed to find the floor if it doesn't fall back down?
How are you moving your character forwards? Post your code.
I just want a constant speed even when my mainCharacter is going up, but because of the physics this is a problem
Please post some code, or illustrate your problem using photo/video. :)
Answer by Vitor_r · May 12, 2014 at 04:15 PM
You can try this. It's not so performance wise, but will do the trick. The player will always be at 20 velocity on the X, and the Y will change accordingly to the jump.
private float acceleration = 20f;
private Vector2 jumpForce = new Vector2(0,10);
void Update(){
rigidbody2D.velocity = new Vector2(acceleration, rigidbody2D.velocity.y);
}
void Jump(){
rigidbody2D.AddForce(jumpForce);
}
i can't test it right now, but i think it will work :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613144820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet not moving from script 3 Answers
2D Collect Item C# Increase Acceleration 2 Answers
Smoothed Rotation with 2D Top-Down View 1 Answer
I only want my character to jump when touching the ground 4 Answers
Multiple Cars not working 1 Answer