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

Problem with z axis rotation

Discussion in 'Scripting' started by Lambdafish, Feb 19, 2017.

  1. Lambdafish

    Lambdafish

    Joined:
    Dec 16, 2016
    Posts:
    7
    Hi, I was wondering if anyone could help with a simple problem I have been having for the past few days. I have a 2.5D game (3D with an orthographic camera) that is set up on the XY axis, and for some reason, whenever I try to rotate objects around the z axis the model does not respond correctly (though x and y axis's do work). My current rotation code is:

    Code (CSharp):
    1. public float range = 15f;
    2. GameObject NearestEnemy = null;
    3.  
    4. void UpdateTarget ()
    5. {
    6.         GameObject Tank = GameObject.FindGameObjectWithTag("Tank");
    7.         float DistancetoTank = Vector3.Distance(transform.position, Tank.transform.position);
    8.         if (DistancetoTank < range)
    9.         {
    10.                 NearestEnemy = Tank;[/INDENT]
    11.         }
    12.         if (NearestEnemy != null)[/INDENT]
    13.         {
    14.                 target = NearestEnemy.transform;
    15.         }
    16.  
    17. void Update()
    18. {
    19.     if(target == null)
    20.     {
    21.             return;
    22.     }
    23.     Vector3 direction = target.position - transform.position;
    24.     Quaternion lookRotation = Quaternion.LookRotation(direction);
    25.     Vector3 rotation = lookRotation.eulerAngles;
    26.     transform.rotation = Quaternion.Euler(0f, 0f, lookRotation.z);
    27. }
    This code should make the object rotate around the z axis to face and follow the "tank" object, but instead it just points north and no longer responds. Could anyone tell me what I am doing wrong, and how to fix it?


    Edit: For added context, this is what is currently happening:



    The pink square is the enemy that I want to rotate to face the "Tank" (the blue character), however it sticks itself in this position when the tank gets in range, only shaking slightly when the tank moves around it.
     
    Last edited: Feb 19, 2017
  2. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    Hi there, and welcome to the forums.
    When posting you can use the code tags if you click the "insert" button while creating a post. This will make it much easier to look at your code :)

    Now for your problem. You have the right idea, but this bit of code has some weird things going on in it:
    These two lines are fine:
    Code (CSharp):
    1. Quaternion lookRotation = Quaternion.LookRotation(direction);
    2. Vector3 rotation = lookRotation.eulerAngles;
    but in the final line
    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0f, 0f, lookRotation.z);
    You reference lookRotation which is a Quaternion, when it appears you meant to reference "rotation", making the line
    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0f, 0f, rotation.z);
     
    Lambdafish likes this.
  3. Lambdafish

    Lambdafish

    Joined:
    Dec 16, 2016
    Posts:
    7
    Thanks for the response, I tried this, but unfortunately it changed nothing, the pink square still points north (up)
     
  4. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    I think the problem stems from Quaternion.LookRotation working where the Y axis is up and the Z axis is forward. If you leave the desired upwards axis out, it defaults to Vector3.up
    Try this:
    Code (CSharp):
    1. Quaternion lookRotation = Quaternion.LookRotation(direction, -Vector3.forward);
    (Note: this assumes the camera is facing in the same direction as the global Z-axis)
     
    Lambdafish likes this.
  5. Lambdafish

    Lambdafish

    Joined:
    Dec 16, 2016
    Posts:
    7
    Thanks! I finally fixed it, I slightly altered your code to get this line, and it worked!

    Code (CSharp):
    1. Quaternion lookRotation = Quaternion.LookRotation(Vector3.forward, direction);