- Home /
How is the following code from 'Space Shooter' Tutorial Working?
I was going through the Space Shooter tutorial when I encountered this code. (in creating shots module)
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float speed;
void Start ()
{
rigidbody.velocity = transform.forward * speed;
}
}
The above code, when run in the editor, works i.e, it moves the bolt forward each frame.
But how is it doing it since it is called in the Start()?
Shouldn't it be called only once? (as per the Execution Order Document?)
From the docs:
Before the first frame update
Start: Start is called before the first frame update only if the script instance is enabled.
Answer by Graham-Dunnett · Aug 02, 2015 at 06:21 PM
If the first time you and me meet I tell you to run at 1 metre per second, then you'll start running. If I never tell you anything different you'll keep running at 1m/s. So, think of our interaction as me telling you in Start() you need to run at a constant speed.
Trivial doubt. Should have searched in scripting docs first. Was confused between rigidbody.velocity and rigidbody.position. Thanks.
Your answer