Search Unity

Having trouble getting a 2d game object to rotate where I want it

Discussion in 'Scripting' started by JapaneseFireBmb, Jul 11, 2020.

  1. JapaneseFireBmb

    JapaneseFireBmb

    Joined:
    Jul 11, 2020
    Posts:
    6
    I'm trying to make a 2d top down pirate ship game. I'm working on the NPC ships and just trying to get them go from one port to another before I start working on getting them to react to the player or anything like that. I'm trying to get them to rotate to point towards the target port and then move them with transform.up. The Interwebs all seem to point to roughly the same thing, but nothing seems to be working quite right. Like it looks like the math to get the angle isn't working right. Does anyone see what my mistake is?

    Vector2 dir = target.transform.position - transform.position;
    float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I recommend using Debug.Log() to output the values of
    dir.x
    and
    dir.y
    and the computed value of
    angle
    to see where things might be going wrong.

    Also, initially just assign the rotation to transform.rotation so that you can reason about it without lerping going on. Isolate the problem spaces until you find the problem.

    The general pattern I use for producing a rotation from an angle is this:

    Code (csharp):
    1. Quaternion rot = Quaternion.Euler( 0, 0, angle);  // angle around the Z+ axis
     
  3. JapaneseFireBmb

    JapaneseFireBmb

    Joined:
    Jul 11, 2020
    Posts:
    6
    Thanks for replying! I really appreciate it.
    So I did what you suggested and got some weird results. It looks like my game object is looking at a different spot than I want it to (tends to be down and to the left) and the angle is off by 90 degrees (When my game object is directly above, it's showing -90 for the angle and pointed to the right).
    I'm not sure how math for Atlan2 or Rad2Deg are supposed to work and REALLY not sure why target.transform.position - transform.position is working right. I saw a few things on the Interwebs breaking that down to
    target.transform.position.x - transform.position.x
    and the same for y, but I'm really not sure why that would make a difference... Thoughts?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    In your original code dir is just used to represent cartesian difference (delta) from one position to the other.

    Atan2() turns that those orthogonal coordintes into an angle, and multiplying it by Rad2Deg makes it degrees.

    if you rotate around the Z axis (as your code suggests with the Angle Axis), the pointing direction will likely be +Y or +X, I'm not sure, I'd have to try it. Whichever one it is, that means your prefab or graphics MUST point in that direction.

    If it does not, reorganize your prefab with an extra top level GameObject so everything else can be rotated to match either the +X or +Y direction, whichever it works out to. I always just fiddle for 30 seconds and re-figure it out again each time I do this, as it varies depending on axis rotated around.
     
  5. JapaneseFireBmb

    JapaneseFireBmb

    Joined:
    Jul 11, 2020
    Posts:
    6
    The rotation on my prefabs have 0's across the board. The asset pack I grabbed had all of the boats facing down, so I put -1 in the Y scale to make them face up instead.
    I had the angle working using:

    Quaternion rotation = Quaternion.AngleAxis(angle - 90f, Vector3.forward);

    earlier. I really don't like that solution though.
    Any idea on why the game object all seem to be looking at a different spot than where they're supposed to be?
     
  6. JapaneseFireBmb

    JapaneseFireBmb

    Joined:
    Jul 11, 2020
    Posts:
    6
    Ok. So I got the angle working right and I've found something supremely weird with the positioning that looks really problematic. So I put the boat on top of where it thinks I'm telling it to go and then I checked that against where it actually should be going and they both have the same transform.positions. Do you have any clue why it's doing that?
     

    Attached Files:

  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    press pause and go digging through the scene and see what's going on!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742