- Home /
Question by
hannacho700 · Nov 29, 2018 at 06:55 AM ·
rigidbodyvelocityforceacceleration
How to change velocity of object which move by Rigidbody.
I made a roller coaster moving along the rails. This roller coaster is driven by a rigid body (kinetic energy, position energy). I want this roller coaster to slow down or speed up when I hit a certain key. How can I do?
(like gas pedal or brake)
Comment
You can use AddForce on rigidbody.
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Best Answer
Answer by mlnczk · Nov 29, 2018 at 07:48 AM
private Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
}
void Update(){
if(Input.GetKeyDown(KeyCode.D){ // we speed up
rb.velocity *= 1.1f;
}else if(Input.GetKeyDown(KeyCode.A) // we slow down
rb.velocity *= 0.9f;
}
}
If you want to go back to normal speed just do rb.velocity *= 1. Im also working on roller coaster and works fine.