Search Unity

Resolved How to get rotation of y-axis of quaternion?

Discussion in 'Entity Component System' started by Samsle, May 31, 2020.

  1. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    Hello,
    I have a cube which is effected by physics. But this cube should only rotate around the y-axis.
    So I was trying to set the rotation component around the x & z-axis to 0. But I don't know how, I tried already so many ways...

    I can get a new quaternion with all rotations set to zero with quaternion.EulerXYZ(0, 0, 0), but for the y-rotation I would somehow need the y-value of the current quaternion and I don't know how.
    The same with inverse: newQuaternion = math.mul(currentQuaternion, math.inverse(currentQuaternion)); no rotation will survive...
     
    Last edited: May 31, 2020
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    The easiest way to freeze rotation around an axis is via the PhysicsMass component:
    physicsMass.InverseInertia = new float3(0, physicsMass.InverseInertia.y, 0);
     
    Samsle likes this.
  3. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    Thank you TRS6123, this works so far.
    But other game logic depending on the physicsMass is now working incorrect. Is there not another way to do it with the plain Rotation component?
     
    Last edited: May 31, 2020
  4. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    Then use one of the RotationEuler components in the Unity.Transforms namespace. Set the x and z components to zero. The main draw back to this is that the code has to run every frame, while setting PhysicsMass.InverseInertia only has to be done once.

    I'm actually curious about this game logic you have that also depends on PhysicsMass
     
    Samsle likes this.
  5. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    Oh my god, I was not aware of these RotationEuler components. I should have read all of the transform system docs :eek:
    These quaternions followed me in my worst dreams ^^
    Thank you so much TRS6123! :)

    And since you were asking:
    I was using this vehicle mechanic for my first game (this), I was just refactoring it to pure ECS. It worked, but the car simulations were to much. I wanted to have more of a "arcade" racing game. So e.g. the car should not overturn in corners. And for this I had the idea of just having a car rotation around the y-axis, so the car is more stable. But the script is using the physicsmass for some calculations.
     
  6. Samsle

    Samsle

    Joined:
    Mar 31, 2020
    Posts:
    110
    I tried
    Code (CSharp):
    1. RotationEulerXYZ myRotate = GetComponent<RotationEulerXYZ>(ce);
    2. myRotate .Value = new float3{x=0, y=myRotate.Value.y, z=0 };
    3. SetComponent<RotationEulerXYZ>(ce, myRotate);
    But it seems that the physics does not affect this component... It has always the same value, so in the end no axis is rotating anymore.