Trying to restrict movement to bounds of screen using MathF.Clamp()
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?
Ive got the code for this actually i wrote it when i was following UdemyComplete Development Course online a few years ago I'll look for it and post it.
ahh bugger, i can't find it i must have deleted it just recently as i had all the scripts together just recently.. damn, also went to redownload the defaults and they removed that as afeature from their updated version of the course... So i'd have to re write the whole thing for you lol Look into ViewPortToWorldPoint() on Udemy API 
This is a screen shot best i could get from video, its missing from top
 using UnityEngine;
 public class Player:$$anonymous$$onoBehaviour{
   [SerializeField]float moveSpeed=10f;
   float x$$anonymous$$in
   floatx$$anonymous$$ax;
   void Start(){
      SetUp$$anonymous$$oveBoundries();
   }
 
                  
and how do they differ:

Answer by DCordoba · Feb 17, 2019 at 12:26 AM
is LateUpdate not
LastUpdate, change thisyou never really get the image bounds,
HorzRangeandVertRangeis still zero, that depends of your own way to proyect the cameravoid LateUpdate() { Camera cam = Camera.main; Vector3 bottomLeft = cam.ScreenToWorldPoint(new Vector3(0f, 0f, transform.position.y)); Vector3 topRight = cam.ScreenToWorldPoint(new Vector3(cam.pixelWidth, cam.pixelHeight, transform.position.y)); //convert to your way to do this HorzRange = new Vector2(bottomLeft.x, topRight.x); VertRange = new Vector2(bottomLeft.y, topRight.y); //I just copy & paste your code here transform.position = new Vector3(Mathf.Clamp ( transform.position.x, HorzRange.x, HorzRange.y), transform.position.y, Mathf.Clamp( transform.position.z, VertRange.x, VertRange.y)); }you dont need to store a
thisTransform, use built in transform
Answer by ray2yar · Feb 16, 2019 at 11:52 PM
Try clamping it before assigning the position. Something like this:
 Vector3 newPos = //clamped values;
 transform.position = newPos;
 
               Also you can take out some of the "fluff". You can refer to the transform of the current gameobject without using get component or that "ThisTransform" reference. Just use: transform.position (or whatever). Also I think you're missing the "z" component in your clamped vector3 (not sure cause my phone is cutting it off). One last thing, any reason this code isn't just in the "Update" ?
I see you defaulted your values to vector2.zero .... So I don't think your player would move at all....
Your answer