Hello guy’s and gal’s.
I have been going through the forums for a good part of the day today and looking at examples of how developers are restricting the movement of a playable character to viewport of the screen. From my understanding this can be done using the camera’s function “View Port to World Point” and the Mathf.Clamp function. However, I have been messing with this all day and for some reason I can just still fly all over the screen with my character. Here is the script that I have created to just test the x values of the clamp. Thank you for looking and I hope someone can help me.
public class scriptCheckBounds : MonoBehaviour {
private float _xMin;
private float _xMax;
private Vector3 _yMin;
private Vector3 _yMax;
private Vector3 _position;
// Use this for initialization
void Start ()
{
_xMin = Camera.main.ViewportToWorldPoint(new Vector3(0, 0)).x;
_xMax = Camera.main.ViewportToWorldPoint(new Vector3(1, 0)).x;
}
// Update is called once per frame
void Update ()
{
_position = transform.position;
_position.x = Mathf.Clamp(_position.x,_xMin,_xMax);
}
}