Search Unity

Use Transform.LookAt and Transform.Rotate at same time

Discussion in 'Scripting' started by saifshk17, Apr 20, 2018.

  1. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Hello,

    When I use transform.LookAt and Transform.Rotate, the rotate function does not work. What am I doing wrong?

    Code (CSharp):
    1.  public GameObject go;
    2.     public GameObject go1;
    3.     public float scaleFactor;
    4.     public float timeFactor;
    5.     public float RotateSpeed;
    6.  
    7. void LateUpdate()
    8.     {
    9.      
    10.         var target = Camera.main.transform.position;
    11.  
    12.  
    13.  
    14.         go.transform.LookAt(target);
    15.         go.transform.rotation *= Quaternion.Euler(0, 180, 0);
    16.  
    17.         go1.transform.LookAt(target);
    18.         go1.transform.rotation *= Quaternion.Euler(0, 180, 0);
    19.  
    20.  
    21.  
    22.         var t = Vector3.forward * Time.deltaTime * RotateSpeed;
    23.         go.transform.Rotate(t);
    24.  
    25.         var t2 = Vector3.back * Time.deltaTime * RotateSpeed;
    26.         go1.transform.Rotate(t2);
    27. }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    What do you mean by "does not work"? What do you expect to happen?

    Note... LookAt sets the rotation so that it looks at 'target'. If you follow that by a slight rotation... you're basically looking at something but slightly off from it. When next LateUpdate comes, LookAt again looks directly at target, and then you slightly shift off of it.

    Over and over you're essentially saying "Look at target, but slightly off".

    If this is not what you want... then this is not what you should be doing.

    What do you expect to happen?
     
  3. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    I want my game object to look at camera along with rotate in the y axis.How do I do this?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    Follow up... this:
    Code (csharp):
    1.  
    2.         go.transform.LookAt(target);
    3.         go.transform.rotation *= Quaternion.Euler(0, 180, 0);
    4.  
    Could be written more efficiently as:
    Code (csharp):
    1. go.transform.rotation = Quaternion.LookRotation(go.transform.position - target.position);
    Since LookAt really is just look in direction of target.pos - go.pos. And the rotate 180 around y, is just flipping it around... so it's go.pos - target.pos.

    Done.


    I don't know what you mean.

    You want to look at something but also rotate at the same time?

    Huh?

    How does one look at something, while also turning? Looking at is a static position, and turning is a dynamic thing.

    ...

    Do you mean you want to 'ease' towards the look rotation over time? So that it doesn't immediately pop to looking at the target, but instead takes a second or so to do so?

    If so, there's lots of options. Such as lerp, slerp, RotateTowards, and easing.

    Here is an example using RotateTowards:
    https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html
    Code (csharp):
    1.  
    2. float speed = 30f; //rotate at 30 degrees per second
    3. var rot = Quaternion.LookRotation(go.transform.position - target.position); //the target look rotation... note the direction is flipped because you have that weird (0,180,0) in your original code
    4. this.transform.rotation = Quatenrion.RotateTowards(this.transform.rotation, rot, speed * Time.deltaTime); //rotate towards over time
    5.  
     
  5. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Thanks for your answers but it did not work for what I was looking for. Please bare with me I shall explain properly.

    Code (CSharp):
    1. var target = Camera.main.transform.position;
    2.  
    3.         go.transform.LookAt(target);
    4.         go.transform.rotation *= Quaternion.Euler(0, 180, 0);
    When I use the above code and move my camera along any axis, the game object also faces the camera as it moves.

    Code (CSharp):
    1. go.transform.Rotate(Vector3.forward * Time.deltaTime * 10);
    Now, when I use only the above code, my game object rotates from left to right continously.

    What I am looking for, is a way to combine both of these, such that when I move my camera, the game object faces along the camera as it moves and also it should rotate in its axis.

    I hope I have made myself clear.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    And did you try this, like I said to in my last post?

    Code (csharp):
    1.  
    2. float speed = 30f; //rotate at 30 degrees per second
    3. var rot = Quaternion.LookRotation(go.transform.position - target.position); //the target look rotation... note the direction is flipped because you have that weird (0,180,0) in your original code
    4. go.transform.rotation = Quatenrion.RotateTowards(go.transform.rotation, rot, speed * Time.deltaTime); //rotate towards over time
    5.  
    Setting speed to 10.
     
  7. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    89
    You could try placing your object as a child of another object and rotate those separately.

    put this in a script in the parent object:

    Code (CSharp):
    1. go.transform.rotation = Quaternion.LookRotation(go.transform.position - target.position);
    2. // OR
    3. go.transform.LookAt(target);
    put this in a script in the child object:

    Code (CSharp):
    1. go.transform.Rotate(Vector3.forward * Time.deltaTime * 10);
    Now the child should rotate around the forward axis in local space while the parent rotates to look at the target.
    You can adjust the rotation of the child in the inspector to align it how you want.
     
    NemoBemo25 likes this.
  8. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Yes, I tried that before. It works :) I was just looking for a way to make it work on a single object.
    Thanks for all your help @lordofduct and @villevli
     
  9. artygrand

    artygrand

    Joined:
    Aug 5, 2019
    Posts:
    4
    Code (CSharp):
    1. public class LookAndRotate: MonoBehaviour
    2. {
    3.     public Transform target;
    4.     private float rot;
    5.  
    6.     private void Update()
    7.     {
    8.         transform.LookAt(target);
    9.         transform.Rotate(0, 0, rot++);
    10.     }
    11. }
    12.