There seem to be a fair amount of questions similar to this but none seem to really answer. I have a gameobject that can jump on a click of a button and automatically keeps going right.
I need the right moving motion to be constant but it ends up building faster and faster over time. If I use Velocity instead of AddForce, the motion is constant. But when I mouse click to jump, it takes like 10 over seconds to reach back down to ground.
Can I please get assistance on how I can keep the automatic movement to the right constant and still able to jump and reach back to ground fast. The following is my code. Thank you.
Edit:
Desired result
- Gameobject constantly moving same speed to the right. When I jump, gameobject jumps and comes back down to ground over a period of 0.5 secs. Jump is expected to be like a smooth flow like a ball that jumps and comes back down smoothly.
Current result Using AddForce to move right
- Gameobject starts to move right slowly and picks up speed over time getting faster and faster thus not able to keep constant same speed.
Current result Using Velocity to move right
- Gameobject able to keep constant speed as wanted, but when I jump the Gameobject jumps and takes about 10 seconds to get back down to ground (It slowly glides back down).
public float jumpSpeed = 300; public float maxSpeed = 15; Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed); } void Update() { if (Input.GetMouseButtonDown(0)) { rb.AddForce(Vector3.up * jumpSpeed); } } void FixedUpdate() { rb.AddForce(Vector3.right * maxSpeed); } //Trying to change velocity instead - Able to keep constant speed but when I jump, takes about 10 secs to get back down to ground. public float jumpSpeed = 2000; public float maxSpeed = 5; Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed); } void Update() { if (Input.GetMouseButtonDown(0)) { rb.AddForce(Vector3.up * jumpSpeed); } } void FixedUpdate() { rb.velocity = new Vector2(5, 0); }