Hello everyone! I’m new to Unity, though I’ve been developing web applications for a while already. I finished the 2D tutorials as a start, but I’m now going through the Space Shooter one to get my feet wet with the engine.
In this particular tutorial:
http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player
Specifically, at about the 15 minute mark, there’s a class called Boundary used to stop the player’s ship from moving out of the camera.
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour {
public float speed, tilt;
public Boundary boundary;
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal"),
moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
rigidbody.velocity = movement * speed;
rigidbody.position = new Vector3(
Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
My question is: When is the Boundary object instantiated? I can see it being declared in line 10 above, but I see no initialization anywhere at all, however, it can be used just fine in lines 19 to 21 without causing an exception or breaking anything. What sorcery is Unity pulling off behind the scenes? And how/when can I consider that Unity will do the same for other custom classes I eventually make?
If I can read about it in the Unity documentation, feel free to point it out to me. I’ve been neglecting it because it’s kinda huge so I’m more comfortable with videos