- Home /
Question by
Xephon99 · Jul 02, 2020 at 07:54 AM ·
addforce movement
addForce to jump issue,addForce jumping problem.
I am new to Unity and coding. I am working on just a simple runner style game and wanted to add a jump feature for the character to get over obstacles and open gaps. The problem I have is if you hold the jump button you just keep getting higher and higher. Is there a way with the code below to apply a negative force after 1 second to ground the player or to stop the upward force after 1 second and not allow it again until the player is grounded?
public class Playermovement : MonoBehaviour { public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
public float jumpForce = 100f;
// Use FixedUpdate instead of Update for Physics based components.
void FixedUpdate()
{
// Add a forward Force
rb.AddForce(0 , 0, forwardForce * Time.deltaTime);
if ( Input.GetKey("d") )
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0 , 0, ForceMode.VelocityChange);
}
if ( Input.GetKey("a") )
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0 , 0, ForceMode.VelocityChange);
}
if ( Input.GetKey("space") )
{
rb.AddForce(0 , jumpForce * Time.deltaTime , 0, ForceMode.Impulse);
}
} }
Comment