Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Entity gets exponentially larger as it is rotated and I don't understand why.

Discussion in 'Project Tiny' started by Raspilicious, Dec 13, 2018.

  1. Raspilicious

    Raspilicious

    Joined:
    Jun 21, 2012
    Posts:
    24
    Hi, all!

    I have been trying to get an entity to rotate and I am getting a weird issue. I have come to the conclusion that I must be going about it the wrong way, but can't for the life of me work out why it's happening.

    When I rotate the entity, it rotates but gets larger exponentially as it continues to rotate. I originally thought that I was somehow changing the scale as well, but the console still outputs (1,1,1).

    While writing this post I had the thought that maybe the code I'm using to rotate the entity is also affecting the scale column of the transform matrix, but even when I output the scale to the console it still shows (1,1,1).

    I've attached a short video below showing some of the code (which is below the video) and what is happening. I removed some of the unnecessary code to make the snippet below a bit shorter (but kept the important parts).

    Additionally, the rotation values aren't showing up correctly in the console log, as you'll see in the video. As the entity rotates, the Z value of the rotation shows numbers between about -2 to 2, but I would expect it to show numbers between -180 and 180.

    Please help!



    Code (CSharp):
    1. namespace game {
    2.     /** New System */
    3.     @ut.executeAfter(ut.Shared.InputFence)
    4.     export class PlayerInputHandler extends ut.ComponentSystem {
    5.         OnUpdate(): void {
    6.             let dt = ut.Time.deltaTime();
    7.  
    8.             this.world.forEach([game.MoveSpeed, game.RotationSpeed, game.MoveWithInput, game.Boundaries,
    9.                 ut.Core2D.TransformLocalPosition, ut.Core2D.TransformLocalRotation, ut.Core2D.TransformLocalScale],
    10.                 (moveSpeed, rotationSpeed, tag, bounds, transformPosition, transformRotation, transformScale) => {
    11.                 // Create local variables to modify.
    12.                 let localPosition = transformPosition.position;
    13.                 let localRotation = transformRotation.rotation;
    14.                 let localScale = transformScale.scale;
    15.  
    16.                 // Rotation
    17.                 // Issue: Entity becomes larger when rotating. :'<
    18.                 if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.A)) {
    19.                     localRotation.z += rotationSpeed.speed * dt;
    20.                 }
    21.                 if (ut.Runtime.Input.getKey(ut.Core2D.KeyCode.D)) {
    22.                     localRotation.z -= rotationSpeed.speed * dt;
    23.                 }
    24.  
    25.                 // Apply our local variables to the respective transform properties.
    26.                 transformRotation.rotation = localRotation;
    27.  
    28.                 // Log some things to the console for debugging.
    29.                 console.log("Rotation:");
    30.                 console.log(localRotation);
    31.                 console.log("Scale:");
    32.                 console.log(localScale);
    33.             });
    34.         }
    35.     }
    36. }
     
  2. L1ancoe

    L1ancoe

    Joined:
    May 23, 2018
    Posts:
    2
    Get same problem, solved using
    Code (JavaScript):
    1. transformRotation.rotation = new Quaternion().setFromAxisAngle(new Vector3(0, 0, -1), Math.PI / 2);
    Intended to rotations Z axis -90 degree.
     
  3. ecurtz

    ecurtz

    Joined:
    May 13, 2009
    Posts:
    640
    No idea if it could cause this, and I haven't actually done anything but read the code sample, but I think Unity quaternions should be normalized?
     
  4. SorneBachse

    SorneBachse

    Joined:
    Dec 27, 2012
    Posts:
    62
    Yea, had the same issue. The rotation in the inspector is shown as an euler angle, but the TransformLocalRotation is stored as a quaternion. So when you manipulate the Z value, it's actually manipulating the Z value of the quaternion which I guess messes up the size of things ¯\_(ツ)_/¯

    Anyways, use L1ancoe's method, works for me as well.
     
  5. Raspilicious

    Raspilicious

    Joined:
    Jun 21, 2012
    Posts:
    24
    Right, I think I'm starting to understand. I can now have my ship face certain directions, but still can't work out how to rotate it over time. I'm quite deep in the Quaternion-rabbit-hole and struggling to understand the finer points of changing them.
     
  6. Nkon

    Nkon

    Unity Technologies

    Joined:
    Jun 12, 2017
    Posts:
    65
    Try modifying your script like this:

    line 13:
    let localRotation = new Euler().setFromQuaternion(transformRotation.rotation);


    line 26:
    transformRotation.rotation = new Quaternion().setFromEuler(localRotation);


    This would convert the rotation from Quaternion to Euler, then set it back after modifying the angle.

    Cheers!
     
  7. Raspilicious

    Raspilicious

    Joined:
    Jun 21, 2012
    Posts:
    24
    Nkon, you are a golden god. It is solved! I didn't think about ways to work out the solution other than trying to work with Quaternions, but you've shown me there can be ways around these things using a bit of lateral thinking. Thank you!!

    Thank you to everybody else who has helped, too. You have all broadened my understanding of Quaternions. :)