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. Dismiss Notice

Question how to add angle to quaternion

Discussion in 'Scripting' started by kader1081, Aug 23, 2023.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    I have a GameObject with a rotation of 0 0 0, and I have a child GameObject with a rotation of -72 0 90. The child object's rotation should be 0 90 90.but i want to add 72 0 90 to targetQuanternion so it it was like 0 90 90 while turning.I've tried using Euler angles and quaternions, but nothing seems to work.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testRotation : MonoBehaviour
    6. {
    7.     Quaternion euler;
    8.     public GameObject toRotate;
    9.    
    10.     // Update is called once per frame
    11.       void Update()
    12.       {
    13.           euler = transform.rotation;
    14.           Quaternion lookRotation = Quaternion.LookRotation(toRotate.transform.position - transform.position);
    15.         Quaternion addedLookRotation = lookRotation * Quaternion.Euler(72f, 0, 90f);
    16.           transform.rotation = Quaternion.RotateTowards(transform.rotation, addedLookRotation, 2f);
    17.       }
    18. }
    19.  
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,913
    Try flipping the order in line 15 (put " * lookRotation" at the back). Rotations are local or global and the order chooses which one. There should be longer explanations of this (I know I've written several).

    For testing, try changing the last line to just "= addedLookrotation;" (comment out the other part). That makes it easier to see if the final angle is correct.
     
    faUnity and Bunny83 like this.
  3. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    I tried everything changing order and adding different things
    . What i found interesting quaternion.euler for(72f,0f,90f) is something like (0.4f,0.6f,-0.6f,0.4f). I have no idea how this quaternion things work. İ mean this shouldnt be that hard i wasted about 8 hours i almost punched my computer
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    Quaternion is 4D mathematical construct, for me it does not sound so easy as I live in 3D :D
     
    Yoreki and Bunny83 like this.
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    the struggle is real..

    But don't ever worry about Quaternions, or try to understand them. Just understand how they work, in relation to making your rotation do what it does. I personally only pay attention to the euler side of things, "it just works!"(tm Todd Howard).

    I'm having trouble picturing this in my head, so chopped it up to read better.. one sec..
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Having a Quaternion field called
    euler
    is a funny misnomer.

    In any case, if you want to produce rotations around an axis, use Quaternion.AngleAxis, rather than euler angles, which will always cause issues.

    Something like this may work:
    Code (CSharp):
    1. private void Update()
    2. {
    3.     Quaternion rotation = transform.rotation;
    4.     Vector3 direction = toRotate.transform.position - transform.position
    5.     Quaternion lookRotation = Quaternion.LookRotation(direction);
    6.  
    7.     Quaternion rightRotation = Quaternion.AngleAxis(72f, Vector3.right);
    8.     Quaternion forwardRoatation = Quaternion.AngleAxis(90f, Vector3.forward);
    9.     Quaternion combinedRotations = rightRotation * forwardRoatation;
    10.  
    11.     Quaternion finalRotation = combinedRotations * lookRotation;
    12.     transform.rotation = Quaternion.RotateTowards(rotation, finalRotation, 2f);
    13. }
     
    wideeyenow_unity likes this.
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    No clue on what you mean, as you add multiple z:90.. But looking at the snippet you showed, see if this isn't what you meant to want?
    Code (CSharp):
    1. public class testRotation : MonoBehaviour
    2. {
    3.     Quaternion myRot;
    4.     public GameObject toRotate;
    5.  
    6.     void Update()
    7.     {
    8.         //myRot = transform.rotation; // not used?
    9.         Vector3 direction = toRotate.transform.position - transform.position;
    10.         Vector3 offset = direction + new Vector3(72f, 0, 90f);
    11.         Quaternion lookRotation = Quaternion.LookRotation(offset, Vector3.up);
    12.         transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, 2f);
    13.     }
    14. }
     
    kader1081 likes this.
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    looking at it that might need some "normalized" in my example, to keep the direction at a value of 1.. Sorry don't have a way to test your exact conditions.

    Also look into using Debug.DrawRay/Line as you can see vector-vector lines or direction "rays" in the scene view
     
    kader1081 likes this.
  9. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    There is an artillery piece. I want to move the artillery barrel towards the enemy. I created the model in Blender. I made 50 cal machine gun falcon and pak gun somehow i made it their barrel looks to enemy but with artillery it wasnt working i moved back to blender after some research i made it.İt works now . I think problem is blender and unity rotation and position calculation are different. exporting from blender to unity really a headache. With 72 0 90 thing normaly barrel looks forward but if i try rotate it like this it doesn't point towards the enemy.. i have to make rotation 0 90 90 but if i make this barrel looks upward
     
  10. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    i searched a lot and i dont know when and why i should eulerangle or angleaxis or other stuff . Maybe i couldnt find it but unity should have better documentation
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    You dont have to understand how quaternions work internally to use them.
    To use them you just need the provided functions and their documentation.

    As a rule of thumb, euler angles only exist for human comprehension. They are a convenience. If you work with rotations, you usually want to just forget them and directly work with quaternions, using the provided API. When you work with euler angles, it should be a read-only process. Either for yourself, or some simple comparisons / thresholds. If you want to write them, you should keep track of the euler angles (persistantly) in the background. Never convert quaternion to euler, work with that, and then write them to quaternions again. A quaternion maps to more than one euler value. The only reason you dont notice that in the inspector, is that unity put extra effort into making sure these stay consistant. For human comprehension, since the inspector is used by humans.

    There is very few, usually highly specialized, people on the entire planet who properly and fully understand quaternions and the actual relation between their x, y, z and w values. It is a topic of complex mathematics (C, the number space above R). A four-dimensional one at that. Pretty hard to mentally grasp, not that i claim i do, despite having worked with several related topics back at uni.

    As for your core problem, this does indeed seem like a problem between the coordinate systems of unity and blender. They use differing coordinate systems, as such.

    Blender:
    X-Axis: Points to the right
    Y-Axis: Points forward
    Z-Axis: Points upwards

    Unity:
    X-Axis: Points to the right
    Y-Axis: Points upwards
    Z-Axis: Points forward

    You have to export models correctly. I rarely if ever work with blender, but there is plenty of tutorials for that. Afterwards just make sure you dont have weird rotations in your object hierarchy. You probably want it to be 0,0,0 before applying rotations. Otherwise you need to put the model in a child to apply a static rotation to it, and then rotate the unrotated parent instead of this child. That way you can apply this static rotation to all custom rotations.
     
    Last edited: Aug 24, 2023
    kader1081 likes this.
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Personally, the answer is you should never use euler stuff, particularly if you know how to use quaternions.

    Ever since I got my head around how to use them, I haven't touched eulers since. It's not that scary or complicated either. You just use the right method to generate the needed rotations, and combine them by multipying them together.
     
    kader1081 likes this.
  13. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    530
    Generating the Quaternion using Quaternion.Euler is fine if that's the most suitable input form for the specific situation. Forcing code to use or FromToRotation, AngleAxis just for the sake of avoiding mention of Euler at any cost (because some one said Euler is bad but you didn't understand why) is silly. Unless you are working with Euler angles defined by some external system with less common axis order or maybe proper Euler angles like XYX or ZXZ, multiplying together result of 3 AngleAxis to simulate Euler angles will only make your code harder to read and probably slower, while producing exactly the same quaternion.

    That said the situations where you need this is significantly less than the amount in which it appears in beginner code.
     
    kader1081 likes this.
  14. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    kader1081 likes this.
  15. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    İ solved it like this but problem is there is no direction for example rotate -90 in x axis but you should know which direction model is looking i think best way to do it test it then you take reference this model direction as reference i am doing like that
    now
     
  16. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    I do not use blender, but as far as I know to fix axes during export you just need to enable some checkbox, can't remember, "apply transforms" or something like that?

    You just need to end up with barrel pointing towards Z axis, so
    transform.forwards
    is aligned with it. This way any rotation should work correctly. Also take into account that when you apply rotations the order is important.
    One additional note - blender default export has problem with nested objects, they are going to be rotated, so if that is the problem you might need export plugin, unless unity import settings checkbox fixes it.
     
    kader1081 likes this.
  17. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    You mean in my post:
    Code (CSharp):
    1. When exporting a FBX model, and wanting perfect Unity rotation:
    2.     - Rotate object -90 on the x axis
    3.     - Ctrl + A(apply changes), select all
    4.     - In export FBX(save screen), Transform settings uncheck "Use Space Transform"
    You can't rotate the object in Blender on it's Xaxis? you can easily, the top right "retractable" transform window, or in the Inspector part to the right also allows you to change position/rotation/scale.
     
    kader1081 likes this.
  18. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,913
    Things like a wrong-axis model are easier to fix without code. Make an empty gameObject named turret. This is the thing you will rotate and aim. Put the model of the turret inside that, as a child. Then note how z is pointing on the empty. Unity thinks +z is forward, so twist the child (the actual barrel model) so it's forward -- probably that (72,0,90) [children show the rotation relative the to parent].

    That essentially bakes-in your (72,0,90) fix rotation. As the parent gets aimed, using a normal lookAt or LookRotation, your child rotation is automatically preserved.
     
    Last edited: Aug 24, 2023
    kader1081 likes this.
  19. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    No i mean lets say my model looks to right but correct direction for exporting to unity is down how can i know because of that i should test which direction is correct one
     
  20. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohhh, I think you mean when playing around in Blender? which way is it's forward, up? If you press "numpad 1" that will move the Blender camera to "forward face" the model. So with numpad 1, if the model is turned to face the camera, then everything is correct.. EXCEPT for the X-axis.. I still have no clue why Blender just can't have the same global definitions of a standard "Vector3", it is well beyond my pay grade... lol

    But with normal camera, rotate the model to look at you, Ctrl + A to apply that as all normal (0,0,0) transforms/rotations/scale, THEN rotate it x:-90, Ctrl +A(again), then export with NO space transform. :)
     
    kader1081 likes this.