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.

Rotation Transformation From Hell

Discussion in 'Scripting' started by Gooren, Jan 20, 2016.

  1. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
    Hello Guys,

    I am using this beauty:
    Code (CSharp):
    1. private float CalculateProjectileFiringSolution()
    2.          {
    3.              var targetTransform = target.position;
    4.              var barrelTransform = barrel.position;
    5.              float y = (barrelTransform.y - targetTransform.y);
    6.              //targetTransform.y = barrelTransform.y = 0;
    7.              float xx = targetTransform.x - barrelTransform.x;
    8.              float xz = targetTransform.z - barrelTransform.z;
    9.              float x = Mathf.Sqrt(xx * xx + xz * xz);
    10.              var v = projectileSpeed;
    11.              var g = Physics.gravity.y;
    12.              var sqrt = (v * v * v * v) - (g * (g * (x * x) + 2 * y * (v * v)));
    13.              if (sqrt < 0)
    14.              {
    15.                  Debug.Log("No Solution");
    16.                  return 0;
    17.              }
    18.              sqrt = Mathf.Sqrt(sqrt);
    19.              //var calculatedAnglePos = Mathf.Atan2(((v * v) + sqrt), (g * x));
    20.              var calculatedAngleNeg = Mathf.Atan2(((v * v) - sqrt), (g * x));
    21.              return calculatedAngleNeg * Mathf.Rad2Deg;
    22.          }
    to calculate world space X axis angle for my tower's gun rotation. My tower is using two transforms in order to aim. First transform (head) to rotate around Y axis and second transform (gun) to rotate around X axis - second transform is child of the first transform. Also, my tower is not stationary, it is placed on a moving vehicle which can also rotate a bit (which is the problem here). Code sample i provided returns world space X axis angle and i need to locally rotate the gun so that it aims correctly for it's target. It is easy and working UNTIL the vehicle that has towers attached to it rotates too (drives over some harsh terrain for example). The problem is that my 'CalculateProjectileFiringSolution' function does not take parental rotations into considerations. And i do not know how to transform this correctly.

    I will literally love You forever if You can help me. Forever... I mean it!

    EDIT: I just realized that head rotation (Y axis) is wrong too because I am rotating it to face the target DIRECTLY - that can be bad for the gun (X axis), because it might not be able to rotate to face FIRING direction correctly using it's local X rotation when the tower's parent (moving vehicle in our case) is rotated somehow weirdly (combination of multipel axes). This is even trickier than I thought.
     
    Last edited: Jan 20, 2016
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  3. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
    Thank You, I tried the code and it's almost what i need. One problem persists though. When I start rotating with the vehicle that has towers attached to it, towers ignore it and stay persistent. More specifically - Y axis rotation segment (head) stays untouched by the rotation of the vehicle. That's because rotation of the head is being performed in the world space. And I don't know how to transform this into local space :(

    EDIT: In the method 'RotateGun' I have changed this:

    Code (CSharp):
    1. xAxisTransform.localEulerAngles = new Vector3(360f - angle.Value, 0f, 0f);
    2. //Rotate the turret towards the target
    3. yAxisTransform.LookAt(target);
    4. yAxisTransform.eulerAngles = new Vector3(0f, yAxisTransform.rotation.eulerAngles.y, 0f);
    to this:

    Code (CSharp):
    1. xAxisTransform.localEulerAngles = new Vector3(360f - angle.Value, 0f, 0f);
    2. //Rotate the turret towards the target
    3. yAxisTransform.LookAt(target, yAxisTransform.parent.up);
    4. yAxisTransform.localEulerAngles = new Vector3(0f, yAxisTransform.localRotation.eulerAngles.y, 0f);
    But now, when i rotate the vehicle around X axis, the gun (X axis rotation) is broken - it is looking in the same direction as before I rotated the vehicle from it's default rotation. Problem is, that the 'CalculateAngleToHitTarget' method returns X axis angle in world space while it does not care about parent object rotation, in fact, about no rotations at all. I am clueless here.
     
    Last edited: Jan 20, 2016
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    could it be as simple as adding an intermediate game object. one that isnt controlled at all, but will be controlled by your script.

    My structure was like this:

    base cube
    -- visible base (squashed cylinder
    -- Turret (empty object centered above the cylinder to act as the thing that makes the next turn) this is what my script is attached to.
    ---- Sphere (the actual gun) attached in teh script as the gun obj
    ------ Cylinder (the barrel of the gun
    ------ Bullet (the thing I fire)
     
  5. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
    I am not sure if I follow. My current setup looks like this:
    Tank
    - Tower head (yAxisTransform; script is attached to this object)
    -- Tower Gun (xAxisTransform; attached to script as a gun object)
    --- Barrel (used only to spawn/shoot projectiles)

    This setup works great - until I start rotating the Tank (root parent object of depicted hierarchy). If I use the script from Your link, the head ignores it's parent's rotation, because the script is fiddling with global rotation of the head object.
    And when I did the modifications I specified in my previous post, the head object persisted it's rotation against it's parent (the tank), but the gun kept aiming in the same local X angle all the time, because it's using calculated world space X angle from the 'CalculateAngleToHitTarget' method. Maybe I just misunderstood what You meant by Your last post, but I just don't know how it could help in my setup when it's already the same basically?

    I am getting so desperate
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    my gun was a sphere. I see where you would have a problem if you had a flat turret like a real tank. I don't know how to ensure that you can handle that.
     
  7. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
    Oh my god... i literally need to have this feature for my game. Please, anyone, help :(

    Anyways - thanks BigMisterB for trying. The link You sent me is a good read also.
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  9. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
    Thank You so much for Your answer! I will try this as soon as I get back home this evening. Have You managed to fix the issue You mentioned in the linked thread by the way? Anyways - if it works, get ready for my love :D
     
  10. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
    @LeftyRighty Your code works like a charm, it's incredible:) Only one problem still remains though. Since I don't understand the code very much, I have no idea how to include resulting angle from method 'CalculateProjectileFiringSolution' (that calculates world X axis angle that the barrel needs to point in in order to hit the target given the bullet drop from Gravity) that is shown in my first post in this thread. Please help.
     
  11. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    300
unityunity