float xMin, xMax, yMin, yMax;
Camera cam;
void Start () {
cam = Camera.main;
xMin = cam.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
xMax = cam.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
yMin = cam.ViewportToWorldPoint(new Vector3(0, 0, 0)).y;
yMax = cam.ViewportToWorldPoint(new Vector3(0, 1, 0)).y;
}
private void Movement4(){
float newX = transform.position.x + GetX();
float newPosX = Mathf.Clamp(newX, xMin, xMax);
transform.position = new Vector3(newPosX, 0, 0);
}
private float GetX(){return Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;}
I wanted an object to keep residing in the screen no matter where the camera’s position is so that I don’t have to rely on raw value (eg. Mathf.Clamp(newX, -10, 10))
I don’t know what’s the problem here
It worked in 2D mode but didn’t in 3D