The camera moves behind the Before - this is the GameObject.
public Transform Before, Camera;
public static volatile bool IsMove = false;
private float Speed = 0f;
private void FixedUpdate() {
Speed = Rb.velocity.magnitude / 0.5f;
IsMove = 1 > Speed;
Camera.position = Vector3.Lerp(Camera.position, Before.position, (Speed <= 0 ? 11 : Speed) * Time.fixedDeltaTime);
}
Everything works fine, except that I can't figure out how to calculate that the camera has approached the boundary of the tirrane and there is no need to move it any further?
CodePudding user response:
A simple idea, do raycast on each corner of the camera with the terrain, if failed, reject the movement.
Camera cam;
Transform camera;
var oldPosition = cameraTransform.position;
cameraTransform.position = newPosition;
int terrainLayer = LayerMask.GetMask("Terrain");
if (!Physics.Raycast(cam.ViewportPointToRay(new Vector3(0, 0, 0)), cam.farClipPlane, terrainLayer))
camera.position = oldPosition ;
else if (!Physics.Raycast(cam.ViewportPointToRay(new Vector3(0, 1, 0)), cam.farClipPlane, terrainLayer))
camera.position = oldPosition ;
else if (!Physics.Raycast(cam.ViewportPointToRay(new Vector3(1, 0, 0)), cam.farClipPlane, terrainLayer))
camera.position = oldPosition ;
else if (!Physics.Raycast(cam.ViewportPointToRay(new Vector3(1, 1, 0)), cam.farClipPlane, terrainLayer))
camera.position = oldPosition ;
