How to add rigid body forces and collision detection to SimpleCameraController

Hello there,

I am looking for a replacement of an older free flight camera (FlyCam) script which seems to be frame-rate dependent and does not give the same navigational experience when used on different computers.

The SimpleCameraController script which comes with Unity does a good job in general but it does not support collision detection with scene objects/colliders.

Could someone please help altering the script so that instead of direct access via transform.position, the position gets changed via application of velocity onto an attached rigidBody component?

I’m sure other users would benefit from this alternative free flight camera script with collision detection.

Thanks in advance!

Greets, B.

Hi there, this can be done by attaching an empty game object with a collider and rigidbody to the camera. Or just add them directly to the camera. I may have miss understood but I don’t think scripts are required.

Having the RB + Collider components attached to the camera doesn’t lead to obstacle avoidance if the camera position gets set directly via transform.position (this is more like a constant teleport and circumvents the collision tests). The system seems to expect velocity added to the RB component in order to work as far as I understood.

I am using this method successfully with an old FlyCam script and would like to update it with the more elaborate methods of the SimpleCameraController.

--------------------old FlyCam script with RB Collision detection but other drawbacks like inconsistency possibly due to frame rate depencies----------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyCam : MonoBehaviour {

Rigidbody rb;

float lookSensitivity = 100;
float speed = 50f;

public Vector2 _smoothMouse;

public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 500;
public float slowMoveFactor = 0.5f;
public float fastMoveFactor = 6;

public float rotationX = 0.0f;
public float rotationY = 0.0f;

public Vector2 sensitivity = new Vector2(0.3f, 0.3f);
public Vector2 smoothing = new Vector2(5, 5);

// Use this for initialization
void Start () 
{
	rb = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () 
{
	if (Input.GetMouseButton(1))
    {
        rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
        rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
        rotationY = Mathf.Clamp(rotationY, -90, 90);
    }
    
    // Get raw mouse input for a cleaner reading on more sensitive mice.
    var mouseDelta = new Vector2(rotationX, rotationY);

    // Scale input against the sensitivity setting and multiply that against the smoothing value.
    mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));

    // Interpolate mouse movement over time to apply smoothing delta.
    _smoothMouse.x = Mathf.Lerp(_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
    _smoothMouse.y = Mathf.Lerp(_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);

	transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
	transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);

	if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
    {
		rb.velocity = Vector3.zero;
        rb.velocity += transform.forward * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
        rb.velocity += transform.right * (normalMoveSpeed * fastMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
    }
    else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
    {
		rb.velocity = Vector3.zero;
        rb.velocity += transform.forward * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Vertical") * Time.deltaTime;
        rb.velocity += transform.right * (normalMoveSpeed * slowMoveFactor) * Input.GetAxis("Horizontal") * Time.deltaTime;
    }
    else
    {
		rb.velocity = Vector3.zero;
		rb.velocity += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
		rb.velocity += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
    }

    if (Input.GetKey(KeyCode.Q)) { transform.position += transform.up * climbSpeed * Time.deltaTime; }
    if (Input.GetKey(KeyCode.E)) { transform.position -= transform.up * climbSpeed * Time.deltaTime; }

}

}