New to Unity.
Working on a 3D scene as a learning project, I’m moving the camera around the player object when the user holds down the middle mouse button and moves the mouse.
I’m trying to limit the vertical movement so that the camera cannot go below the player (y=0).
The idea was that I would copy the camera transform, then do a RotateAround on the copy. If the result was > 0 then it would be safe to do the RotateAround on the ‘real’ transform. My thinking was that doing the operation on the real thing then resetting it to 0 if required may cause flickering or jumping.
But it doesn’t work - the RotateAround on the copy also affects the real one. Is it passing by reference when setting the copy of the transform?
//rotate camera around player if middle mouse button pressed and mouse moved
if (Input.GetMouseButton(2))
{
float rotateHorizontal = Input.GetAxis("Mouse X");
float rotateVertical = Input.GetAxis("Mouse Y");
// horizontal rotation
transform.RotateAround(player.transform.position, -Vector3.up, rotateHorizontal * sensitivity);
//vertical rotation
checkTransform = transform;
checkTransform.RotateAround(Vector3.zero, checkTransform.right, rotateVertical * sensitivity);
if (checkTransform.position.y > 0)
{
transform.RotateAround(Vector3.zero, transform.right, rotateVertical * sensitivity);
}
}
checkTransform is defined as private Transform checkTransform;
and sensitivity is a public variable