Home > Enterprise >  FlyCamera with collision
FlyCamera with collision

Time:12-20

How to create collisions for a flying camera in Unity 3d? to prevent the camera from falling into objects on the scene. My camera script snippet:

public class FlyCamera : MonoBehaviour
{
void FixedUpdate()
{
   if (!isAlternative)
        SetCameraMovement();
}
private void SetCameraMovement()
{
    lastMouse = Input.mousePosition - lastMouse;
    lastMouse = new Vector3(-lastMouse.y * camSens, lastMouse.x * camSens, 0);
    lastMouse = new Vector3(transform.eulerAngles.x   lastMouse.x, transform.eulerAngles.y   lastMouse.y, 0);
    if (Input.GetMouseButton(1))
        transform.eulerAngles = lastMouse;
    lastMouse = Input.mousePosition;
    GetBaseInput();
    transform.position = Vector3.Lerp(transform.position, NewPosition, Time.deltaTime * movementTime);
}

private void GetBaseInput()
{
    Speed = Input.GetKey(KeyCode.LeftShift) ? superSpeed : mainSpeed;
    if (Input.GetKey(KeyCode.W))
        NewPosition  = transform.forward * Speed;
    if (Input.GetKey(KeyCode.S))
        NewPosition  = transform.forward * -Speed;
    if (Input.GetKey(KeyCode.A))
        NewPosition  = transform.right * -Speed;
    if (Input.GetKey(KeyCode.D))
        NewPosition  = transform.right * Speed;
}

}

CodePudding user response:

Better put you camera movement in the LateUpdate() as adviced in the docs.

With a rigidbody and a collider properly set for the collision detection. Check docs so that you can detect the collisions properly.

Check that " Notes: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached", so your camera could be one gameObject with no rigidbody, so that you can fly free and collide.

I case you might need physics behaviour for yout camera and you want it also to behave as a rigidbody I would set the camera movement with rigidbody.SetPosition instead of with transform.position = new Vector3(), as you need to choose to set the transform in the geometric or the physics world. Also you would need to uncheck the gravity option not to fall.

CodePudding user response:

If you want to achieve a RTS-like flying camera, you can set the camera to a parent dolly-like object and keep it looking at that object:

Object (Camera Dolly) -> this is moved by wasd and is rotated to the desired view angle

Child object: Camera -> only the camera's position.y is ever changed

Then, on a custom script on the dolly in Update(), send a LineCast from the dolly to the camera. When the cast hits a collider (important to use e.g. tags here so it does not collide with anything, like e.g. units) in the world, gradually (over time) reduce the camera's position.y and if nothing is hit, gradually increase it to the desired height. It will not entirely prevent the camera from clipping trough bigger objects but move it so it doesn't stay "inside" a collider. Something like that is also typically used in RPG third person cameras.

Please let me know if more clarification is needed!

Edit: I'd do that only for really big view-blocking objects though, not for the terrain or smaller buildings/units - at least if your goal is more of an RTS or sim game.

  • Related