Search Unity

GameObject transform rotation sometimes returns Quaternion.identity

Discussion in 'Scripting' started by stach78, Sep 25, 2021.

  1. stach78

    stach78

    Joined:
    Aug 21, 2017
    Posts:
    5
    Hello everyone,

    I create projectiles in following way:

    Code (CSharp):
    1.  GameObject projectile = GameObject.Instantiate(WeaponType.projectileTemplate, FirePoint.transform.position, FirePoint.transform.rotation);
    Where FirePoint is barrel end of a weapon. If the weapon is a laser, the projectile is a long straight line from FirePoint.transform.position in the direction of Firepoint.Transform.rotation.

    Most of the time this works perfectly. However sometimes (it seems this happens more often when a lot of stuff is going on and gpu or cpu is very busy) the rotation quaternion of Firepoint gameobject returns (0.0, 0.0, 0.0, 1.0) and the ray appears shooting straight up (I have top view in my project, even though it is in 3D). In the very next frame the rotation is back to correct.

    In game it manifests like this:
    problem.jpg

    What can be the cause and how to fix this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Sounds like something is making your firepoint rotation invalid! You didn't post that code... are you setting the Transform.forward when the forward vector is zero? That would probably do it, as that operation is obviously undefined.

    ALSO: Dig your UI and color scheming. Very sharp!
     
  3. stach78

    stach78

    Joined:
    Aug 21, 2017
    Posts:
    5
    Thank you for quick answer and for kind word! The mech rotates towards mouse pointer, it is done like that (executed in Update()):
    Code (CSharp):
    1.     void Cs2dStyleRotate(Vector3 mousePos)
    2.     {
    3.         Vector3 objectPos = Camera.main.WorldToScreenPoint(cyborgTopPart.transform.position);
    4.         mousePos.x = mousePos.x - objectPos.x;
    5.         mousePos.y = mousePos.y - objectPos.y;
    6.  
    7.         float angle = (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg) - 90;
    8.        
    9.         cyborgTopPart.transform.rotation = Quaternion.Euler(new Vector3(0, -angle, 0));
    10.     }
    To be exact, gameobject that represents top part of the mech is rotated, and attached to it are gameobjects that represent weapons. Logs indicate that the rotation of the 'top part' is sometimes incorrect. In no other place in code is the rotation of this object changed.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    If mousePos is Vector3.zero, the problem you report would happen.

    Don't set rotation if mousePos.magnitude is below a certain tiny amount, such as 0.01f
     
  5. stach78

    stach78

    Joined:
    Aug 21, 2017
    Posts:
    5
    I've added an if in Update():
    Code (CSharp):
    1. Vector3 mousePos = Input.mousePosition;
    2. if(mousePos.magnitude > 0.1f && mousePos != Vector3.zero) {
    3.     Cs2dStyleRotate(mousePos);
    4. }
    but the problem persists (I've tried different values for magnitude, no improvement).
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Now try commenting out the line that sets rotation, play for a bit with a non-rotating player. Does the error still happen? Then who is setting the rotation?

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    stach78 and Bunny83 like this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,999
    It's not your mouse position that must not be 0, it's your mousePos variable inside our method when you use Atan2 that must not be 0 ^^. If both x and y are 0 you run into issues. That happens when your mouse is exactly on the object since you subtract the object position from your mouse position.

    Though of course there could be other issues. Have you actually tried debugging the issue? Adding some Debug.Logs before your Instantiate call to see what rotation you actually pass in? Also is it possible that there's a acript on the object you instantiate that could modify the rotation of the object?
     
    stach78 likes this.
  8. stach78

    stach78

    Joined:
    Aug 21, 2017
    Posts:
    5
    Yes, I've added quite a few logs, the rotation passed to Instantiate is "rotation: (0.0, 0.0, 0.0, -1.0)". I am fairly sure that x and y are not zero, so the problem is probably not there. Right now I am going through other components and scripts attached to gameobject to check if anything is modifying the rotation, as you suggested.
     
  9. stach78

    stach78

    Joined:
    Aug 21, 2017
    Posts:
    5
    OK, found it - animator component resets rotation of my mech's top part to align it with the rest of mech. I have to change the animation, apparently. Thanks guys for assistance!