Using Instantiate() to spawn objects
Now
within this IF
statement—meaning after the opening {
and before the closing },
put the following line:
C#:
Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
Javascript:
var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
Here we are creating a new variable named instance
. Into this variable we are storing a reference to the creation of a new object that is of type Rigidbody
.
The Instantiate commands requires three pieces of information namely, Instantiate(What to make, Where to make it, a rotation to give it);
So in our example, we are telling our script to create an instance of whatever object or prefab is assigned to the bullet
public member variable and that we would like it to be created using the values of position and rotation from the transform component of the object this script is attached to—the Main
Camera. This is why you will often see transform.position
written...