Search Unity

sphere collider and scripts

Discussion in 'Scripting' started by kattkieru, Mar 4, 2008.

  1. kattkieru

    kattkieru

    Joined:
    Jan 2, 2006
    Posts:
    130
    Hi all,

    I have two scripts. One is a main controller for an animation, and the other is a camera script. The camera uses bot lateupdate and fixedupdate -- if you're in flight mode the motion is handled by a rigid body in fixedupdate, but if you're not in flight mode lateupdate makes the camera orbit locators according to mouse rotations.

    Now, I wanted to give the camera the ability to collide with things so that I can have it limited to the area of the animation and not have it pass through the floor. However, when I add a sphere collider to the camera I get null object references in the animation controller. Suddenly it can't find my "crash_camera" class on the camera gameobject. If I delete the sphere collider from the camera then everything goes back to working as expected.

    Not sure what I've done wrong here -- shouldn't you be able to add a sphere collider and have it work the way I'm thinking? Sorry if this is a terribly noobish question, but I haven't really gotten into the rigidbody and collider stuff until today. Ideally I'd like to have the ground be a mesh collider (it's modeled, not a terrain object), and to have some colliders floating at the edges to keep the camera inside.

    Thanks in advance, and here's the camera code in case the problem is there:

    Code (csharp):
    1.  
    2. // cribbed from the standard componetns
    3. // modified for more interactivity
    4.  
    5. var originArray : Transform[];
    6. var targetArray : Transform[];
    7.  
    8. var startCam : int = 0;
    9.  
    10. var xSpeed = 250.0;
    11. var ySpeed = 120.0;
    12.  
    13. var yMinLimit = 20;
    14. var yMaxLimit = 80;
    15.  
    16. // flight vars
    17. var inFlight : boolean = false;
    18. var thrust = 200.0;
    19. var moveDrag = 1;
    20. var stopDrag = 5;
    21. var flightYMinLimit = -90;
    22. var flightYMaxLimit = 90;
    23.  
    24. var godNode : cwLocator;
    25.  
    26. private var origin : Transform;
    27. private var originData : cwLocator;
    28. private var target : Transform;
    29.  
    30. private var currentCam;
    31.  
    32. private var distance : float = 10.0;
    33. private var x : float = 0.0;
    34. private var y : float = 0.0;
    35.  
    36. private var mouse1Down : boolean = false;
    37. private var shiftDown : boolean = false;
    38.  
    39.  
    40. function ResetPosition() {
    41.     // moves camera back to its origin and aims it at the target
    42.     transform.position = origin.position;
    43.     transform.rotation = Quaternion.identity;
    44.     transform.LookAt(target.position);
    45.    
    46.     var angles = transform.eulerAngles;
    47.     x = angles.y;
    48.     y = angles.x;
    49.  
    50.     distance = Vector3.Distance(target.position, transform.position);
    51.    
    52.     // finally, make sure we're not flying
    53.     inFlight = false;
    54.     rigidbody.isKinematic = true;
    55.     rigidbody.detectCollisions = false;
    56. }
    57.  
    58.  
    59. function Start () {
    60.     // make sure we start off with a real camera
    61.     origin = originArray[startCam];
    62.     originData = origin.GetComponent(cwLocator);
    63.     target = targetArray[startCam];
    64.    
    65.     currentCam = startCam;
    66.  
    67.     // Make the rigid body not change rotation
    68.     rigidbody.freezeRotation = true;
    69.        
    70.     // do a quick position reset
    71.     ResetPosition();
    72. }
    73.  
    74. function LateUpdate () {
    75.     /*  LateUpdate handles the regular camera motions
    76.         when flying, the camera is moved in FixedUpdate by its
    77.         rigid body.
    78.     */
    79.    
    80.     var l_xSpeed = xSpeed;
    81.     var l_ySpeed = ySpeed;
    82.    
    83.     var position;
    84.     var rotation;
    85.    
    86.     var angles = transform.eulerAngles;
    87.     x = angles.y;
    88.     y = angles.x;
    89.    
    90.     shiftDown = Input.GetButton("LeftShift");
    91.    
    92.     // emulate the faster movement of the Unity GUI
    93.     if (shiftDown) {
    94.         l_xSpeed *= 2;
    95.         l_ySpeed *= 2;
    96.     }  
    97.    
    98.     mouse1Down = Input.GetButton("LeftMouse");
    99.    
    100.     // fix for certain view situations where mouse input makes the view jump
    101.     // try 2: movement deadzone
    102.  
    103.     // change motion by camera type
    104.     if (!inFlight) {
    105.         if (originData.data1 == 0)
    106.         {
    107.             x += Input.GetAxis("Mouse X") * l_xSpeed * 0.02;
    108.             y -= Input.GetAxis("Mouse Y") * l_ySpeed * 0.02;
    109.  
    110.             y = ClampAngle(y, yMinLimit, yMaxLimit);
    111.                
    112.             if (target  mouse1Down) {
    113.                 rotation = Quaternion.Euler(y, x, 0);
    114.                 position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    115.        
    116.                 transform.rotation = rotation;
    117.                 transform.position = position;
    118.             }
    119.         }
    120.    
    121.         else if (originData.data1 == 1) {
    122.             transform.position = origin.position;
    123.  
    124.             if (mouse1Down) {
    125.                 x += Input.GetAxis("Mouse X") * (l_xSpeed / 1.25) * 0.02;
    126.                 y -= Input.GetAxis("Mouse Y") * (l_ySpeed / 1.25) * 0.02;
    127.  
    128.                 y = ClampAngle(y, yMinLimit, yMaxLimit);
    129.                
    130.                 transform.rotation = Quaternion.Euler(y, x, 0);
    131.             }
    132.         }
    133.     }
    134.    
    135.     // last minute check for flight
    136.     if (Input.GetButton("StartFlying"))
    137.         StartFlying();
    138. }
    139.  
    140. function FixedUpdate () {
    141.  
    142.     var vertForce : int = 0;
    143.     var horizForce : int = 0;
    144.  
    145.     var SHIP_MAX_FORCE : int = 600;
    146.  
    147.     // this code was adapted from the marble tutorial PDF
    148.     if (inFlight) {
    149.         var l_xSpeed = xSpeed;
    150.         var l_ySpeed = ySpeed;
    151.  
    152.         var horizontal = Input.GetAxis("Horizontal");
    153.         var vertical = Input.GetAxis("Vertical");
    154.         shiftDown = Input.GetButton("LeftShift");
    155.  
    156.         var right = transform.TransformDirection(Vector3.right);
    157.         var forward = transform.forward;
    158.    
    159.         var forwardForce = forward * vertical * thrust;
    160.         rigidbody.AddForce(forwardForce);
    161.    
    162.         var rightForce = right * horizontal * thrust;
    163.         rigidbody.AddForce(rightForce);
    164.  
    165.         // Apply drag only if the user is not pressing any keys
    166.         if (vertical == 0  horizontal == 0)
    167.            rigidbody.drag = stopDrag;
    168.         else
    169.            rigidbody.drag = moveDrag;
    170.  
    171.         // apply rotations
    172.         if (mouse1Down) {
    173.             x += Input.GetAxis("Mouse X") * (l_xSpeed) * 0.02 * (shiftDown ? 2 : 1);
    174.             y -= Input.GetAxis("Mouse Y") * (l_ySpeed) * 0.02 * (shiftDown ? 2 : 1);
    175.  
    176. //          y = ClampAngle(y, flightYMinLimit, flightYMaxLimit);
    177.                
    178.             transform.rotation = Quaternion.Euler(y, x, 0);
    179.         }
    180.     }
    181. }
    182.  
    183. static function ClampAngle (angle : float, min : float, max : float) {
    184.     if (angle < -360)
    185.         angle += 360;
    186.     if (angle > 360)
    187.         angle -= 360;
    188.     return Mathf.Clamp (angle, min, max);
    189. }
    190.  
    191. function SwitchCamera() {
    192.     currentCam ++;
    193.     if (currentCam == originArray.length)
    194.         currentCam = 0;
    195.  
    196.     origin = originArray[currentCam];
    197.     target = targetArray[currentCam];
    198.     originData = origin.GetComponent(cwLocator);
    199.    
    200.     ResetPosition();
    201. }
    202.  
    203. function StartFlying() {
    204.     inFlight = true;
    205.     rigidbody.isKinematic = false;
    206.     rigidbody.detectCollisions = true;
    207. }
    208.