Search Unity

Smooth look at

Discussion in 'Scripting' started by radiolobito, Jul 7, 2009.

  1. radiolobito

    radiolobito

    Joined:
    Jun 12, 2009
    Posts:
    117
    How can I make a smooth rotate to object?

    this is the code that i'm using

    Code (csharp):
    1. var localizeobject : Transform;
    2. var localize = false;
    3.  
    4. function Update () {
    5. if (localize == true){
    6. transform.LookAt(localizeobject);
    7. localize = false;
    8. }
    9. else
    10. {
    11. }
    12. }
    13.  
    14. function OnGUI () {
    15.     if (GUI.Button (Rect (10,100,150,100), "Localize Object")) {
    16.         localize = true;
    17.     }
    18. }
    This causes a instant rotation to the object... how can i make this rotation smooth?
     
  2. jaxas

    jaxas

    Joined:
    Mar 22, 2009
    Posts:
    59
    try this:
    Code (csharp):
    1.  
    2. var targetObj : GameObject;
    3. var speed : int = 5;
    4.  
    5. function Update(){
    6. var targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);
    7.        
    8. // Smoothly rotate towards the target point.
    9. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
    10. }
    11.  
    ;)
     
  3. Ineno

    Ineno

    Joined:
    Jul 7, 2009
    Posts:
    2
    Do you have a smart way of detecting if the camera finished the movement? So that I can take away control of the camera while it's doing the smooth look at but give it back once it's done looking at that certain object.
     
  4. radiolobito

    radiolobito

    Joined:
    Jun 12, 2009
    Posts:
    117
    It's workin great!

    I think this is a great solution, I'm agree with Ineno; i need to know when the rotation finish to stop this automatic rotation and make the manual rotation again.
     
  5. ieselisra

    ieselisra

    Joined:
    Nov 6, 2015
    Posts:
    1
    Works Like a Charm (Unity 5.5.2)
     
  6. WalterEspinar

    WalterEspinar

    Joined:
    Aug 23, 2011
    Posts:
    80
    1. Code (CSharp):
      1. public GameObject targetObj;
      2. public int = 5;
      3.  
      4. void Update()
      5. {
      6. Quaternion targetRotation = Quaternion.LookRotation(targetObj.transform.position - transform.position);
      7.      
      8. // Smoothly rotate towards the target point.
      9. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
      10. }
     
  7. riboslem_unity

    riboslem_unity

    Joined:
    Oct 25, 2018
    Posts:
    1
    you can use a raycast to determine if you are pointing to the desired object and so to know if the rotation is finished (maybe wait 0.2s (depends of the rotation speed) to let it looks at the center of the object)
     
  8. dudeLzy

    dudeLzy

    Joined:
    Feb 16, 2018
    Posts:
    1
    I think I am pretty late maybe, but instead of raycast, u can turn this into a coroutine and then know if it has finished, something like this

    Code (CSharp):
    1. IEnumerator RotateTransform(float dur)
    2.        float t = 0f;
    3.        while(t < dur){
    4.               t += Time.deltaTime;
    5.               float factor = t / dur;//use this for lerping
    6.               yield return null;//basically wait for next frame
    7.         }
    8.         print("Rotation Finished");
    9. }
     
  9. DranrebKing

    DranrebKing

    Joined:
    Jun 3, 2018
    Posts:
    4
    This still works! Thanks a lot!
     
  10. silvabrid_unity

    silvabrid_unity

    Joined:
    Nov 14, 2019
    Posts:
    1
    what can i use to limit rotation on the smooth look at script??