So, I’m a newbie who just started Unity. I made a game where you just dodge obstacles with a cube, and everything is going fine until when I added a jump option. To keep it moving at one direction, I used AddForce every FixedUpdate on the z axis, and the player IS moving at a constant speed, but when it jumps, the player accelerates. I think it is because of the lack of drag.
Here is my script for PlayerMovement:
{
public Rigidbody rb;
public float forwardForce = 700f;
public float sidwaysForce = 100f;
public Vector3 jump;
public float jumpForce = 3.0f;
public bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay()
{
isGrounded = true;
}
void OnCollisionExit()
{
isGrounded = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
rb.AddForce(sidwaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidwaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
,So, I’m a newbie that is trying to make a simple game where you just move a cube and dodge obstacles. the cube moves forwards via adding force on the z axis every FixedUpdate. Now this is working fine, but after I made a jump option, when the gameObject is in the air there are no drag, causing it to accelerate. The acceleration gets balanced out slowly after it hits the ground again, but if you keep spamming jump it will just get faster and faster.