Search Unity

Keeping distance

Discussion in 'Scripting' started by dacloo, Jun 16, 2006.

  1. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi,

    I am adjusting the SmoothLookat script so that after smooth rotating / lookingat, the camera has to keep distance from the player (inspector variables distanceY and distanceZ).

    But the smooth lookat is "ruined" because I reposition the camera afterwards. Here is where I am stuck.

    How can I move the camera away from its target?
    In Blitz Basic, I would use "MoveEntity (x,y,z)", where I say: "move relative to its current orientation". But here I am using absolute vectors, which is wrong.
    Ideas?

    Thanks

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var damping = 6.0;
    4. var smooth = true;
    5. var distanceY : float;
    6. var distanceZ : float;
    7.  
    8. @AddComponentMenu("Camera-Control/Smooth Look At")
    9. partial class SmoothLookAt { }
    10.  
    11. function LateUpdate () {
    12.     if (target) {
    13.         if (smooth) {
    14.             // Look at and dampen the rotation
    15.             var rotation = Quaternion.LookRotation(target.position - transform.position);
    16.             transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    17.  
    18. // lines below ruins lookat
    19.             transform.position = target.transform.position;
    20.             transform.position.y += distanceY;
    21.             transform.position.z += distanceZ;
    22.            
    23.             }
    24.         else {
    25.             // Just lookat
    26.             transform.LookAt(target);
    27.             }
    28.         }
    29.    
    30.     }
    31.  
    32.  
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Try repositioning your camera _before_ you make it look at your target. Doing it in reverse is probably your problem.

    -Jeremy
     
  3. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Jeremy,

    That doesn't solve the problem.
    Because the camera is placed behind a fixed Z and X of the character,
    the Slerp or Lookat does not visually work anymore.

    What I want is to place the camera behind the character,
    but the X and Z should be relative to its current orientation.

    I think the solution is somewhere in RotateAround, but I how
    do I slerp the camera from its current rotation to Vector.back of
    the character? How do I translate the coordinates?

    Below is a siuation I am looking for
    above is a "normal" situation where the player is walking forward,
    but when he turns into a direction, the camera should reposition itself
    behind the player, smoothly (in my case I want this with a slight delay,
    not like smoothLookAt does).
    In all cases I want the camera to keep a fixed Z and X distance.
     

    Attached Files:

  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You just call transform.Translate this is relative to the orientation.

    Code (csharp):
    1.  
    2. transform.position = target.transform.position;
    3. transform.Translate(0, distanceY, distanceZ);
    4.  

    Alternatively you can also multiply by the rotation eg:

    Code (csharp):
    1.  
    2. transform.position = target.transform.position;
    3. transform.position += rotation * Vector3(0, distanceY, distanceZ);
    4.  
     
  5. dacloo

    dacloo

    Joined:
    Jun 30, 2005
    Posts:
    469
    Hi Joachim,

    In this case, where is 'rotation' based on? Is it the lookrotation? Because the camera goes beserk now :)

    Code (csharp):
    1.  
    2. var target : Transform;
    3. var damping = 6.0;
    4. var distanceY : float;
    5. var distanceZ : float;
    6.  
    7. @AddComponentMenu("Camera-Control/Smooth Look At Jeroen")
    8. partial class SmoothLookAt { }
    9.  
    10. function LateUpdate () {
    11.    if (target) {
    12.         // Look at and dampen the rotation
    13.         var rotation = Quaternion.LookRotation(target.position - transform.position);
    14.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    15.  
    16.         transform.position = target.transform.position;
    17.         transform.position += rotation * Vector3(0, distanceY, 0-distanceZ);   
    18.  
    19.  
    20.       }
    21.    }
    22.  
    Check out what happens: http://www.stickystudios.com/unity

    Tnx.