Search Unity

Camera Collision Script Help

Discussion in 'Scripting' started by Poopaloop, Dec 10, 2013.

  1. Poopaloop

    Poopaloop

    Joined:
    Aug 1, 2013
    Posts:
    21
    Hey guys, I found a camera collision script that I like the only problem is the script is hooked up with a "var target : Transform"
    What I need it to do however is search for a tag target instead

    So this is the original code that I found here on the forums (I would have asked there but the thread was from like 2007, didn't wanna revive an old one).

    Code (csharp):
    1. // The target we are following
    2.  
    3. var target : Transform;
    4.  
    5. // The distance in the x-z plane to the target
    6.  
    7. var distance = 10.0;
    8.  
    9. // the height we want the camera to be above the target
    10.  
    11. var height = 5.0;
    12.  
    13. // How much we
    14.  
    15. var heightDamping = 2.0;
    16.  
    17. var rotationDamping = 3.0;
    18.  
    19.  
    20.  
    21. //---New code---
    22.  
    23.  
    24.  
    25. //how close does the camera have to get to a wall before it will bounce
    26.  
    27. var bounceRange : float = 0.5;
    28.  
    29. var bounceAmt : float = 20;
    30.  
    31.  
    32.  
    33. //---End of new code---
    34.  
    35.  
    36.  
    37. function LateUpdate () {
    38.  
    39.     // Early out if we don't have a target
    40.  
    41.     if (!target)
    42.  
    43.         return;
    44.  
    45.    
    46.  
    47.     // Calculate the current rotation angles
    48.  
    49.     wantedRotationAngle = target.eulerAngles.y;
    50.  
    51.     wantedHeight = target.position.y + height;
    52.  
    53.        
    54.  
    55.     currentRotationAngle = transform.eulerAngles.y;
    56.  
    57.     currentHeight = transform.position.y;
    58.  
    59.  
    60.  
    61. //---New/altered code---
    62.  
    63.  
    64.  
    65. //Cast rays to the left and right of the camera, to detect walls.
    66.  
    67.     if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), bounceRange)) {
    68.  
    69.         currentRotationAngle -= bounceAmt;
    70.  
    71.     } else if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), bounceRange)) {
    72.  
    73.         currentRotationAngle += bounceAmt;
    74.  
    75. //If no walls are detected, execute the normal follow behavior.
    76.  
    77.     } else {
    78.  
    79.         // Damp the rotation around the y-axis
    80.  
    81.         currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    82.  
    83.     }
    84.  
    85.  
    86.  
    87. //---End of new code---
    88.  
    89.    
    90.  
    91.     // Damp the height
    92.  
    93.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    94.  
    95.  
    96.  
    97.     // Convert the angle into a rotation
    98.  
    99.     // The quaternion interface uses radians not degrees so we need to convert from degrees to radians
    100.  
    101.     currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0);
    102.  
    103.    
    104.  
    105.     // Set the position of the camera on the x-z plane to:
    106.  
    107.     // distance meters behind the target
    108.  
    109.     transform.position = target.position;
    110.  
    111.     transform.position -= currentRotation * Vector3.forward * distance;
    112.  
    113.  
    114.  
    115.     // Set the height of the camera
    116.  
    117.     transform.position.y = currentHeight;
    118.  
    119.    
    120.  
    121.     // Always look at the target
    122.  
    123.     transform.LookAt (target);
    124.  
    125. }
    I tried adding the target tags myself but when I start the game up it doesn't go to the target. Here's my attempt at trying to fix it myself

    Code (csharp):
    1. // The target we are following
    2.  
    3. var TargetTag : String = "Player";
    4. private var target : GameObject;
    5.  
    6. // The distance in the x-z plane to the target
    7.  
    8. var distance = 10.0;
    9.  
    10. // the height we want the camera to be above the target
    11.  
    12. var height = 5.0;
    13.  
    14. // How much we
    15.  
    16. var heightDamping = 2.0;
    17.  
    18. var rotationDamping = 3.0;
    19.  
    20.  
    21.  
    22. //---New code---
    23.  
    24.  
    25.  
    26. //how close does the camera have to get to a wall before it will bounce
    27.  
    28. var bounceRange : float = 0.5;
    29.  
    30. var bounceAmt : float = 20;
    31.  
    32.  
    33.  
    34. //---End of new code---
    35.  
    36.  
    37.  
    38. function LateUpdate () {
    39.  
    40.     // Early out if we don't have a target
    41.  
    42.     if (!target)
    43.  
    44.         return;
    45.  
    46.    
    47.  
    48.     // Calculate the current rotation angles
    49.  
    50.     wantedRotationAngle = target.eulerAngles.y;
    51.  
    52.     wantedHeight = target.position.y + height;
    53.  
    54.        
    55.  
    56.     currentRotationAngle = transform.eulerAngles.y;
    57.  
    58.     currentHeight = transform.position.y;
    59.  
    60.  
    61.  
    62. //---New/altered code---
    63.  
    64.  
    65.  
    66. //Cast rays to the left and right of the camera, to detect walls.
    67.  
    68.     if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), bounceRange)) {
    69.  
    70.         currentRotationAngle -= bounceAmt;
    71.  
    72.     } else if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), bounceRange)) {
    73.  
    74.         currentRotationAngle += bounceAmt;
    75.  
    76. //If no walls are detected, execute the normal follow behavior.
    77.  
    78.     } else {
    79.  
    80.         // Damp the rotation around the y-axis
    81.  
    82.         currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    83.  
    84.     }
    85.  
    86.  
    87.  
    88. //---End of new code---
    89.  
    90.    
    91.  
    92.     // Damp the height
    93.  
    94.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    95.  
    96.  
    97.  
    98.     // Convert the angle into a rotation
    99.  
    100.     // The quaternion interface uses radians not degrees so we need to convert from degrees to radians
    101.  
    102.     currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0);
    103.  
    104.    
    105.  
    106.     // Set the position of the camera on the x-z plane to:
    107.  
    108.     // distance meters behind the target
    109.  
    110.     transform.position = target.position;
    111.  
    112.     transform.position -= currentRotation * Vector3.forward * distance;
    113.  
    114.  
    115.  
    116.     // Set the height of the camera
    117.  
    118.     transform.position.y = currentHeight;
    119.  
    120. }
    So could someone help me fix it up?
    Thanks in advance.
     
  2. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    It looks like you've defined a TargetTag string for the script but didn't really do anything with it, so it just sits there. Without purpose. Unappreciated and misunderstood.
    Left alone to stare into the void and silently ask itself: "Why?"
    ...
    You'll be wanting to pass it to the FindWithTag method, which will return a single active GameObject tagged with the matching string (or null if none are found, so you probably want to account for that). You can assign that to your target Transform err... that is, change your target GameObject back to a Transform type and assign it the .transform property of the result of the FindWithTag method. And you'll be probably be wanting to do that only once on Start.

    See if that doesn't get it working the way you want.
     
  3. Poopaloop

    Poopaloop

    Joined:
    Aug 1, 2013
    Posts:
    21
    Ha xD that made me chuckle.
    I tried and failed to add what you said. errors came up whenever I added something and tried to fix tho but the same result came, the cam wouldn't lock onto the player (not a scripter). Thanks for the pointer though man, I appreciate it. :)
     
  4. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    There were a few missing variable declarations in the script, but I think what you want is something like this:
    Code (csharp):
    1.  
    2. #pragma strict
    3.    
    4. //The target we are following
    5. var targetTag : String;
    6. var target : Transform;
    7.      
    8. //The distance in the x-z plane to the target
    9. var distance = 10.0;
    10.      
    11. //The height we want the camera to be above the target
    12. var height = 5.0;
    13.      
    14. //How much we
    15. var heightDamping = 2.0;
    16. var rotationDamping = 3.0;
    17.      
    18. //---New code---
    19.      
    20. //how close does the camera have to get to a wall before it will bounce
    21. var bounceRange : float = 0.5;
    22. var bounceAmt : float = 20;
    23.    
    24. function Start()
    25. {
    26.     if(targetTag != null  target == null)
    27.     {
    28.         var taggedObject = GameObject.FindWithTag(targetTag);
    29.         if(taggedObject != null)
    30.         {
    31.             target = taggedObject.transform;
    32.         }
    33.     }
    34. }
    35.      
    36. //---End of new code---
    37.      
    38. function LateUpdate()
    39. {
    40.     // Early out if we don't have a target
    41.     if(!target)
    42.         return;
    43.      
    44.     // Calculate the current rotation angles
    45.     var wantedRotationAngle = target.eulerAngles.y;
    46.     var wantedHeight = target.position.y + height;
    47.      
    48.     var currentRotationAngle = transform.eulerAngles.y;
    49.     var currentHeight = transform.position.y;
    50.      
    51.     //---New/altered code---
    52.      
    53.     //Cast rays to the left and right of the camera, to detect walls.
    54.     if(Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.right), bounceRange))
    55.     {
    56.         currentRotationAngle -= bounceAmt;
    57.     }
    58.     else if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), bounceRange))
    59.     {
    60.         currentRotationAngle += bounceAmt;
    61.     }
    62.     //If no walls are detected, execute the normal follow behavior.
    63.     else
    64.     {
    65.         // Damp the rotation around the y-axis
    66.         currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    67.     }
    68.      
    69.     //---End of new code---
    70.     // Damp the height
    71.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    72.      
    73.     // Convert the angle into a rotation
    74.     // The quaternion interface uses radians not degrees so we need to convert from degrees to radians
    75.     var currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0);
    76.      
    77.     // Set the position of the camera on the x-z plane to:
    78.     // distance meters behind the target
    79.     transform.position = target.position;
    80.     transform.position -= currentRotation * Vector3.forward * distance;
    81.      
    82.     // Set the height of the camera
    83.     transform.position.y = currentHeight;
    84.      
    85.     // Always look at the target
    86.     transform.LookAt (target);
    87. }
    88.  
    The Start function implements the usage of FindWithTag that I had mentioned.
    I didn't look at the other details specific to this camera script, but it looks like it should work as intended now.
     
  5. Poopaloop

    Poopaloop

    Joined:
    Aug 1, 2013
    Posts:
    21
    Hey man, totes didn't expect you to have fixed it up for me but thanks a lot for that. I'll give it a go and see how it works now. :)
     
  6. Roland1234

    Roland1234

    Joined:
    Nov 21, 2012
    Posts:
    190
    For sure - let me know how it works and I'll try to fix it if it doesn't.