Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Using Quaternion Euler to slowly rotate towards an object.

Discussion in 'Scripting' started by Geeknerd1337, Jan 30, 2018.

  1. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    51
    Using the following code to rotate towards the player on the Y-axis only.
    ```
    Vector3 vector = Player.position - transform.position;
    //Vector3.RotateTowards(transform.forward, vector, 1 * Time.deltaTime, 0.0f)
    Quaternion LookAtRotation = Quaternion.LookRotation(vector);
    Quaternion LookAtRotationOnly_Y = Quaternion.Euler(transform.rotation.eulerAngles.x, LookAtRotation.eulerAngles.y + 90, transform.rotation.eulerAngles.z);

    ```
    Before, I used the commented out code inside the loocRotation to rotate it slowly, but putting in place of vector doesn't seem to do anything.

    So what's my issue here, does the rotate towards need to be somewhere else?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Using code tags:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    Code (csharp):
    1.  
    2. Vector3 vector = Player.position - transform.position;
    3. //Vector3.RotateTowards(transform.forward, vector, 1 * Time.deltaTime, 0.0f)
    4. Quaternion LookAtRotation = Quaternion.LookRotation(vector);
    5. Quaternion LookAtRotationOnly_Y = Quaternion.Euler(transform.rotation.eulerAngles.x, LookAtRotation.eulerAngles.y + 90, transform.rotation.eulerAngles.z);
    6.  
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    As for your problem.

    I don't see where you set the rotation back to the target.

    Also, if you want this to happen over time, this needs to be in an Update/Corouitne, or other sort of method that occurs over a duration of time.
     
  4. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    51
    Sorry for the coding tags, got too used to how discord handles them, it's in the Update event, and what do you mean by setting the rotation back to the target?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    All you've done is create a Quaternion.

    What do you do with that Quaternion?

    My point was mainly that you really didn't show any context to the code. I get now that it was in update, so I presume you use LookAtRotationOnly_Y to then set something's rotation.
     
  6. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    51
    Code (CSharp):
    1.     void Update()
    2.     {
    3.  
    4.         Vector3 vector = Player.position - transform.position;
    5.         //Vector3.RotateTowards(transform.forward, vector, 1 * Time.deltaTime, 0.0f)
    6.         Quaternion LookAtRotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, vector, 1 * Time.deltaTime, 0.0f));
    7.         Quaternion LookAtRotationOnly_Y = Quaternion.Euler(transform.rotation.eulerAngles.x, LookAtRotation.eulerAngles.y + 90, transform.rotation.eulerAngles.z);
    8.  
    9.         transform.rotation = LookAtRotationOnly_Y;
    10. }
    Thats the full function. Sorry about the mess.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    eating lunch now, will look at soon
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you're looking to ignore the direction's y value, you can zero that out. Then, using look rotation to the 'vector', you can try Quaternion.RotateTowards
     
    lordofduct likes this.
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Without knowing the specifics of what you mean by "rotate around the y-axis" (globally? locally? something else?)

    The best thing I could say is to repeat methos5k.

    Get the direction vector, redact the y-value, rotate to that.
     
  10. Geeknerd1337

    Geeknerd1337

    Joined:
    Jun 5, 2013
    Posts:
    51
    This was along the local y of the object the script is on. Basically I'm trying to get an enemy to face a player at a specific speed.