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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Quaternion.LookRotation sometimes backwards?

Discussion in '2D' started by itzclay36, Jul 6, 2015.

  1. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Hello, I am working on a 2d game that uses Quaternion.LookRotation in order to aim at the player and shoot.

    I do this by using a starting point object inside the character that rotates towards the player and then shoots based on that angle. I also keep track if the object has been flipped or not, and then using + or - the speed for the projectile.

    This code actually works for the most part and does the job usually.

    Code (CSharp):
    1.         Quaternion rotation = Quaternion.LookRotation(collider.transform.position - kunaiStart.position, kunaiStart.TransformDirection(Vector3.up));
    2.         kunaiStart.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
    But I noticed that at times it would start to fire backwards. After a few hours of studying and changing the speed and everything else, I finally figured out that as long as I am pretty much equal in height with the player, the aiming mechanism works without problem.

    However, if the player is close to being above or below this mob, then it will suddenly fire backwards. And it will maintain that backwards firing until the player is then below him again(AI has cooldown and only reaims as needed).

    I finally figured out that the aiming object(has no visual representation) was reversing itself when it aimed above or below with the LookRotation code above.

    Has anyone experienced this before and can direct me towards fixing it? As long as I am on the same general level it always works fine, and when I get to the right place above and/or below it always reverses.

    My flip code is the standard localscale flip. This works fine, even when the player is below etc.

    Code (CSharp):
    1. _self.transform.localScale = new Vector3(-1, 1, 1);
    But it must be clashing with the LookRotation or something, I just can't figure out where/how.

    Thanks.
     
  2. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Hmmm no help. I am terrible with words, did I screw up the explanation?
     
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Are you sure you are using Quaternion.LookRotation() correctly? I can't look at my code right now, but I believe the documentation for the method is quite misleading. If I remember correctly, it rotates the object so that its forward vector points into the direction given as the "forward" parameter, and that its upward vector points into the direction given as the "upward" parameter. The forward vector is usually in the Z direction, which is meaningless for 2D mode. This means that the "forward" parameter should always be Vector3.forward.

    I believe your rotation should look like this:

    Code (csharp):
    1. Quaternion.LookRotation(Vector3.forward, collider.transform.position - kunaiStart.position)
     
    Hathakas likes this.
  4. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Thanks, but that also didn't work. I did however end up finding a solution that was completely different, but seems to be working great. Much easier to work with as well, and I no longer need to keep track of direction for bullets.

    Code (CSharp):
    1.         Vector3 diff = collider.transform.position - kunaiStart.position;
    2.         diff.Normalize();
    3.         float rotZ = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    4.         kunaiStart.rotation = Quaternion.Euler (0f, 0f, rotZ);
     
  5. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Can you be more specific? In particular, what results do you see, and what do the axes of your object look like? Do they usually point in the same directions as the world XYZ directions?
     
  6. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Sure. It was just shooting at different places which seemed kind of random. I'm sure there was a pattern to it, but it wasn't apparent easily.

    I then changed the code to this, keeping with what you said about Vector3.forward:

    Code (CSharp):
    1.     Quaternion.LookRotation(collider.transform.position - kunaiStart.position, Vector3.forward )
    2.  
    And had somewhat better results. It was keeping with the left and right just fine and did not seem to bug out like it had been. However, it was shooting in the opposite direction relative to up and down. When I was above the mob, it would shoot towards the ground, and when I was below it, then it would shoot above.

    After a bit more studying, I believe it may have been the line under it that was causing trouble. I read other people who mentioned having troubles when they tried to zero out parts of a quaternion.

    Code (CSharp):
    1.  kunaiStart.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
    That is what lead me to trying a different method completely.

    And yes on the axes. There is no character rotation, so always the same.
     
    rubble1 likes this.