Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera collision with rigidbody velocity

Discussion in 'Scripting' started by GabeF, Jan 16, 2013.

  1. GabeF

    GabeF

    Joined:
    Sep 28, 2012
    Posts:
    38
    Hi Guys,

    I have been doing some trials with camera collisions, and the most useful method I have found is to suggest to the camera where I want to go instead of forcing it. I do this by using the velocity factor to specify a velocity in the direction that I'm going.

    This is great for the rigid body, and it moves just fine, it even collides and stops really nicely with world geometry, but my next problem is that I am actually using the camera orbit script to move the camera, and just telling the rigid body that I'd like it to try and move there.

    So essentially, the rigid body veers off when it cannot go to the same place, and i can never make it re-group with the camera transform when out of the collision vicinity. I tried to lerp the camera position in LateUpdate(), but because this mouseorbit sets the x and y values through GetAxis("MouseX") and GetAxis("MouseY"), It actually stores these x and y values to set the position.

    So what I'd really like is to move the camera somewhere via it's transform.position parameters, and have that automatically update the settings in the x and y. - Does that make sense? I really hope so.

    Here is the current script.
    Code (csharp):
    1.  
    2. var target : Transform;
    3. var distance = 10.0;
    4.  
    5. var xSpeed = 250.0;
    6. var ySpeed = 120.0;
    7.  
    8. var yMinLimit = -20;
    9. var yMaxLimit = 80;
    10.  
    11. var distanceMin = 3;
    12. var distanceMax = 15;
    13.  
    14. var childdirection : Transform; // the rigidbody object that tries to follow
    15.  
    16. private var x = 0.0;
    17. private var y = 0.0;
    18. private var lastposition : Vector3;
    19. private var curVelocity : Vector3;
    20.  
    21.  
    22.  
    23.  
    24. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    25.  
    26.  // this camera script aims to suggest a movement by adding velocity onto rigid bodies. This allows for much cleaner collisions to occur
    27.    
    28. function Start () {
    29.     var angles = transform.eulerAngles;
    30.     x = angles.y;
    31.     y = angles.x;
    32.     // set start for rigid body
    33.     childdirection.transform.position = transform.position;
    34.     childdirection.rotation = transform.rotation;
    35.     // Make the rigid body not change rotation
    36.     if (rigidbody)
    37.         rigidbody.freezeRotation = true;
    38.     lastposition = transform.position;
    39.     CurrentVelocity();
    40. }
    41.  
    42. function FixedUpdate () {
    43.  
    44.     if (target)
    45.     {
    46.         if(Input.GetMouseButton(0))
    47.         {
    48.         OrbitCam();
    49.         }
    50.     }
    51.  
    52. }
    53.  
    54. function LateUpdate()
    55. {
    56.     // make the camera try and reach the position of the collision sphere
    57.     transform.position = Vector3.Lerp(transform.position,childdirection.position,Time.deltaTime * 10);
    58.     ConvertAngles(transform);
    59. }
    60.  
    61.  
    62. function OrbitCam()
    63. {
    64.         x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
    65.         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    66.  
    67.         y = ClampAngle(y, yMinLimit, yMaxLimit);
    68.        
    69.         var rotation = Quaternion.Euler(y, x, 0);
    70.  
    71.         //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
    72.  
    73.  
    74.         var position = rotation * Vector3(0.0, 0.0, -distance);// + target.position;
    75.         transform.rotation = rotation;
    76.         transform.position = position;
    77.        
    78.         CurrentVelocity();
    79.         childdirection.rigidbody.velocity = curVelocity;
    80.         childdirection.rotation = transform.rotation;
    81.         lastposition = transform.position;
    82.  
    83. }
    84.    
    85. function CurrentVelocity() // store current velocity
    86. {
    87. curVelocity = (transform.position - lastposition)/ Time.deltaTime;
    88. }
    89.  
    90. function ConvertAngles(trns : Transform)
    91. {
    92.     var angles = trns.eulerAngles;
    93.     x = angles.y;
    94.     y = angles.x;
    95. }
    96.  
    97. static function ClampAngle (angle : float, min : float, max : float) {
    98.     if (angle < -360)
    99.         angle += 360;
    100.     if (angle > 360)
    101.         angle -= 360;
    102.     return Mathf.Clamp (angle, min, max);
    103. }
    104.  
    105.