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): var target : Transform; var damping = 6.0; var smooth = true; var distanceY : float; var distanceZ : float; @AddComponentMenu("Camera-Control/Smooth Look At") partial class SmoothLookAt { } function LateUpdate () { if (target) { if (smooth) { // Look at and dampen the rotation var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); // lines below ruins lookat transform.position = target.transform.position; transform.position.y += distanceY; transform.position.z += distanceZ; } else { // Just lookat transform.LookAt(target); } } }
Try repositioning your camera _before_ you make it look at your target. Doing it in reverse is probably your problem. -Jeremy
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.
You just call transform.Translate this is relative to the orientation. Code (csharp): transform.position = target.transform.position; transform.Translate(0, distanceY, distanceZ); Alternatively you can also multiply by the rotation eg: Code (csharp): transform.position = target.transform.position; transform.position += rotation * Vector3(0, distanceY, distanceZ);
Hi Joachim, In this case, where is 'rotation' based on? Is it the lookrotation? Because the camera goes beserk now Code (csharp): var target : Transform; var damping = 6.0; var distanceY : float; var distanceZ : float; @AddComponentMenu("Camera-Control/Smooth Look At Jeroen") partial class SmoothLookAt { } function LateUpdate () { if (target) { // Look at and dampen the rotation var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); transform.position = target.transform.position; transform.position += rotation * Vector3(0, distanceY, 0-distanceZ); } } Check out what happens: http://www.stickystudios.com/unity Tnx.