I making a 2D game. I want it to have a constant speed. Equal at all times. I’m not sure how to do this. I’ve tried AddForce and Velocity but it always runs out. I add a speed and then it slows down over time. I’ve tried adding it once in a while but it either loses too much speed in the end or gains to much speed. I’m making one of the running games that need a even speed like Subway Surfers, Slither.io, and Dune. I also need jumping but it can’t be constant because it shouldn’t just fly into the sky lol. But, I also need that because with force it depends on how early they jump and it is hard because it can’t just fly into the sky but it shouldn’t depend on how early. My Code:
using UnityEngine;
public class PlayerForce : MonoBehaviour {
public int movementForce = 10;
public Rigidbody2D rb2D;
private void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
void OnTriggerEnter2D(Collider2D other)
{
rb2D.velocity = new Vector3(movementForce * Time.deltaTime, 0, 0);
}
}
Jumping
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && Jumping == false)
{
rb2D.AddForce(transform.up * jump * Time.deltaTime);
Invoke("PushDown", pushdowndelay);
Jumping = true;
Invoke("TurnJumpingOn", 1f);
}
}
void TurnJumpingOn()
{
Jumping = false;
}
void PushDown()
{
rb2D.AddForce(transform.up * pushDown * Time.deltaTime);
}