- Home /
How Do I Increase the ForwardForce of the player over time? using rigid body
Hello, I am trying to increase the forward force of the player when a milestone is set, that each time the x- axis is increased the more forward force the player will receive. I have also a score that shows how far the player is on the X-axis.
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public Rigidbody rb;
public float forwardForce = 2000f;
public float slidewaysForce = 500f;
public float upwardForce = 10f;
// Update is called once per frame
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
rb.AddForce(slidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce (-slidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("w"))
{
rb.AddForce (0, upwardForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, -upwardForce * Time.deltaTime, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GamerManager>().EndGame();
}
}
}
Answer by JxWolfe · Apr 09, 2020 at 10:18 PM
void Update(){
if(gameObject.transform.position.x > milestone)
{
forwardForce+=10;
milestone += milestoneIncremtDistance;
}
}
Long story short, this increases your forward force, every time you travel 1 milestoneIncremtDistance.
Answer by logicandchaos · Apr 10, 2020 at 12:21 AM
you can use GetKeyDown instead, you may have to decrease the force and or add more drag to the rigidbody
Your answer
Follow this Question
Related Questions
Rigidbody speed in some senes different?! 1 Answer
Keeping momentum with rigid bodies. 0 Answers
Linear Velocity 1 Answer
Comparing rigidbody speeds 1 Answer