• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by vectorvis · May 06, 2020 at 03:26 PM · cameracollisionrigidbodyvelocityflying

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CSPAG · May 06, 2020 at 05:46 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by vectorvis · May 07, 2020 at 09:26 AM

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; }

 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

333 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers

Projectile throw in 3rd person 0 Answers

OnTriggerEnter doesn't work if the colliding object is not moving. Translating it by (0,0,0) fixes it. What's the problem? 4 Answers

How to trigger a BoxCollider with a RigidBody? 0 Answers

Is there any good sample code for shooting a projectile and collision detection? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges