- Home /
Question by
jayamohanan96 · Jun 16, 2018 at 08:51 PM ·
updateforcestartorder-of-execution
A force is applied to a stationary rigid body. Velocity before and after application of force is printed but both values are shown as zero. Why?
public class example : MonoBehaviour {
Rigidbody rb;
Vector3 force;
void Start () {
force = new Vector3(5f, 0, 0);
rb = GetComponent<Rigidbody>();
print("(Start) Velocity before force applied " + rb.velocity);//this gives value (0,0,0)
rb.AddForce(force, ForceMode.Force);
print("(Start) Velocity after force applied " + rb.velocity);//this gives value (0,0,0)
}
void Update () {
print("(Update) Velocity after force applied " + rb.velocity);//this gives value (0.1,0,0)
}
}
Comment
Answer by Hoax1 · Jun 22, 2018 at 03:44 AM
I'm pretty sure thats because void Start ()
is only executing the code on one frame and AddForce()
doesn't set the velocity instantly, it accelerates the rigidbody over multiple frames. By for example setting the velocity directly to your force
variable, you can see that the second print gives a value of (5.0, 0.0, 0.0).
Your answer
Follow this Question
Related Questions
Start, Awake, Update. Any other ways to call functions from an empty GameObject? 3 Answers
Question about Unity documentation GameObject.Find 2 Answers
How to update new frame while another is computing? 0 Answers
Using start function after loading level a second time 0 Answers
Random number being generated 60 times when I only want one. 1 Answer