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.

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


    Jun 21, 2012
    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();
    8.   [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;
    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.                 }
    25.                 // Apply our local variables to the respective transform properties.
    26.                 transformRotation.rotation = localRotation;
    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


    May 23, 2018
    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


    May 13, 2009
    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


    Dec 27, 2012
    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


    Jun 21, 2012
    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


    Unity Technologies

    Jun 12, 2017
    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.

  7. Raspilicious


    Jun 21, 2012
    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. :)