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

(Closed) Dude! The target lost the transform object!

Discussion in 'Scripting' started by chips, Dec 13, 2007.

  1. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    I'm adaptating the SuperMarioController and Camera but I had an weird problem.

    when I use the code:
    Code (csharp):
    1.  
    2.     print("OnFollowCamera111");
    3.         // Early out if we don't have a target
    4.  
    5.     if (!target){
    6.         return;
    7.     }
    8.  
    9.     print("OnFollowCamera");
    10.    
    11.     // Calculate the current rotation angles
    12.     wantedRotationAngle = target.eulerAngles.y;
    13.     wantedHeight = target.position.y + height;
    14.        
    15.     currentRotationAngle = transform.eulerAngles.y;
    16.     currentHeight = transform.position.y;
    17.    
    18.     // Damp the rotation around the y-axis
    19.     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    20.  
    21.     // Damp the height
    22.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    23.  
    24.     // Convert the angle into a rotation
    25.     // The quaternion interface uses radians not degrees so we need to convert from degrees to radians
    26.     currentRotation = Quaternion.EulerAngles (0, currentRotationAngle * Mathf.Deg2Rad, 0);
    27.    
    28.     // Set the position of the camera on the x-z plane to:
    29.     // distance meters behind the target
    30.     transform.position = target.position;
    31.     transform.position -= currentRotation * Vector3.forward * distance;
    32.  
    33.     // Set the height of the camera
    34.     transform.position.y = currentHeight;
    35.    
    36.     // Always look at the target
    37.     transform.LookAt (target);
    38.  
    at the update(), everything is just fine and the camera follows whatever it has to follow.

    But when the exactly same code is inside a function, the target gets null or kind of... The thing is that the camera simply don't move.

    By the way, the decaration of the target is:
    Code (csharp):
    1.  
    2.  
    3. // The target we are following
    4. var target : Transform;
    5.  
    6.  
    Any clue about what is going on?

    TXS!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're not moving the "var target : Transform;" line, are you? That has to stay outside any functions if you want it to be a public variable.

    --Eric
     
  3. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    I believe that I'm not moving the var target to anywere. it has been declared on the first lines of the script, out of any function
    Code (csharp):
    1. var target : Transform;
    and I'm using the target inside the functions...[/quote]