Search Unity

How to apply a dampening effect to the Transform.LookAt rotation?

Discussion in 'Scripting' started by guitarxe, Dec 20, 2014.

  1. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    I want an object to follow another using Transform.LookAt. However, I do not want the rotation from LookAt to be instant. I want there to be a maximum angle it can turn in any one frame. Example - A missile following a ship. It should be turning rather slowly towards the target and not instantly.
    How can I accomplish that?

    I have tried taking the difference between the current transform's rotation angle, and the angle the transform is at when rotated using LookAt and then applying the difference as the rotation amount, clamped between a min and max. However, I have run into problems when rotations happen around 0 degrees, going from negative to positive, and have been so far unable to solve those (my missile flips wildly when the object that it is following goes from positive to negative).
     
  2. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    Please show your code. it would be easier to help you.


    with out seeing your code
    Code (CSharp):
    1. transform.rotation =Quaternion.Slerp(transform.rotation, rotation,Time.deltaTime/dampen;
     
    Last edited: Dec 20, 2014
  3. TheFreedomBird

    TheFreedomBird

    Joined:
    Dec 19, 2014
    Posts:
    8
    Code (CSharp):
    1. public class DungeonCrawlerCamera : MonoBehaviour {
    2.  
    3. public GameObject target;
    4. public float damping = 1;
    5. Vector3 offset;
    6.  
    7. void Start () {
    8.  
    9. offset = transform.position - target.transform.position;
    10.  
    11. }
    12.  
    13.  
    14. void LateUpdate () {
    15.  
    16. Vector3 desiredPostion = target.transform.position + offset;
    17. transform.position = desiredPostion;
    18. Vector3 position = Vector3.Lerp (transform.position, desiredPostion, Time.deltaTime * damping);
    19. transform.position = position;
    20.  
    21. transform.LookAt (target.transform.position);
    22.  
    23. }
    24. }
    This code is for a dungeon crawler camera and the way its set up is you can change the damping effect in the game engine itself. This code with also track the object you want it to when you apply it to your camera. Hope it helps
     
    Last edited: Dec 20, 2014
  4. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    Fire bird thanks for helping him, but please use code tags, make it easier to read. Its under the sticky for how to use tags. Thanks,
     
  5. TheFreedomBird

    TheFreedomBird

    Joined:
    Dec 19, 2014
    Posts:
    8
    I it fixed thanks for telling me that.
     
    Last edited: Dec 20, 2014
  6. recon0303

    recon0303

    Joined:
    Apr 20, 2014
    Posts:
    1,634
    np .

    /cheers
     
  7. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Thank you for the replies

    This works well, but I don't understand how to actually dampen how fast it can rotate. I've tried values between 1 and 100, and between 0 and 1.

    This one, likewise, when I convert it to use rotation, does not seem to have any effect with the dampen variable.

    And this was the code that I wrote. The dampen for this actually works, but if the target rotation goes from 359 to 1, it flips out and starts doing weird things. I just don't know 3D vectors that well to understand what I'm doing wrong.

    Code (csharp):
    1.  
    2. public GameObject target;
    3.   public float flightTime = 10f;
    4.   public float flightSpeed = 10f;
    5.   public float maxTurnAngle = 10f;
    6.   private Vector3 dir;
    7.  
    8.   void Start()
    9.   {
    10.   //orient towards target
    11.   transform.LookAt(target.transform);
    12.   //set velocity
    13.   rigidbody.velocity = transform.forward * flightSpeed;
    14.   }
    15.  
    16.   void Update()
    17.   {
    18.   //set direction towards target
    19.   dir = target.transform.position - transform.position;
    20.   //current rotation
    21.   Quaternion currentRotation = transform.rotation;
    22.   //current rotation angles
    23.   float currentAngleY = transform.rotation.eulerAngles.y;
    24.   float currentAngleX = transform.rotation.eulerAngles.x;
    25.   //orient towards target
    26.   transform.LookAt(target.transform);
    27.   //get new angles for when oriented towards target
    28.   float newAngleY = transform.rotation.eulerAngles.y;
    29.   float newAngleX = transform.rotation.eulerAngles.x;
    30.   //get differences between current and new angles
    31.   float differenceY = newAngleY - currentAngleY;
    32.   float differenceX = newAngleX - currentAngleX;
    33.   //set rotation back to original
    34.   transform.rotation = currentRotation;
    35.   //apply dampen for Y rotation
    36.   float rotateByY = Mathf.Clamp(Mathf.Abs(differenceY), 0f, maxTurnAngle);
    37.   if (differenceY < 0f)
    38.   {
    39.   rotateByY = rotateByY * -1f;
    40.   }
    41.   //apply dampen for X rotation
    42.   float rotateByX = Mathf.Clamp(Mathf.Abs(differenceX), 0f, maxTurnAngle);
    43.   if (differenceX < 0f)
    44.   {
    45.   rotateByX = rotateByX * -1f;
    46.   }
    47.   //rotate
    48.   transform.Rotate(rotateByX, rotateByY, 0f);
    49.   //set new velocity
    50.   rigidbody.velocity = transform.forward * flightSpeed;
    51.   }
     
  8. TheFreedomBird

    TheFreedomBird

    Joined:
    Dec 19, 2014
    Posts:
    8
    I'm pretty sure the damping doesn't have anything to do with how fast the camera rotates but more of how smoothly it rotates.
     
  9. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Ohhh, I must have misrepresented what I'm trying to do. Sorry, me no speaka da English so well, haha.

    What I'm trying to do is to simulate a missile fired at the player who controls a helicopter. So I don't want it to be able to track the player with 100% accuracy, so that the player is able to dodge it.

    For that purpose I am trying to rotate the object to face the player, but limit how fast it can rotate. That is what I meant when I said "dampen".