I’m new to Unity and am following a book with examples. This one creates a spaceship shooter game. I want to restrict the spaceship to only be able to go within the screen view. I’ve used the following code:
public class BoundsLock : MonoBehaviour
{
private Transform ThisTransform = null;
// can be changed from inspector
public Vector2 HorzRange = Vector2.zero;
public Vector2 VertRange = Vector2.zero;
// use this for initialization
void Awake()
{
ThisTransform = GetComponent<Transform>();
}
// Update is called once per frame
void LastUpdate()
{
// clamp position
ThisTransform.position = new Vector3(Mathf.Clamp
(ThisTransform.position.x, HorzRange.x, HorzRange.y),
ThisTransform.position.y,
Mathf.Clamp(ThisTransform.position.z, VertRange.x, VertRange.y));
}
}
Problem is the ship still goes out of the screen and gets lost. What am I doing wrong?