Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Rotate GameObject around point in 2D Top-Down

Discussion in 'Scripting' started by VeryGoodDeveloperISwear, Nov 30, 2023.

  1. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    56
    I have an enemy that has an attack AOE (area of effect, it is a trigger polygon collider) in which any objects will get damaged when the enemy attacks. I want the AOE to rotate towards the target (the player) around the enemy, but I can't figure out how to do so. (I don't want the enemy itself to rotate because it would ruin the pixel art sprites)

    Here's what I've tried so far:
    Code (CSharp):
    1. float _zRotation = 0f;
    2.             //in 1st or 4th quadrants
    3.             if (player.position.x >= trans.position.x)
    4.             {
    5.                 _zRotation = Mathf.Asin(player.position.normalized.y - trans.position.normalized.y) * Mathf.Rad2Deg;
    6.             }
    7.  
    8.             else
    9.             {
    10.                 //in 2nd quadrant
    11.                 if (player.position.y >= trans.position.y)
    12.                 {
    13.                     _zRotation = 180f - (Mathf.Asin(player.position.normalized.y - trans.position.normalized.y) * Mathf.Rad2Deg);
    14.                 }
    15.  
    16.                 //in 3rd quadrant
    17.                 else
    18.                 {
    19.                     _zRotation = (Mathf.Asin(player.position.normalized.y - trans.position.normalized.y) * Mathf.Rad2Deg) + 180f;
    20.                 }
    21.             }
    22.  
    23.             attackAOE.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
    24.             attackAOE.transform.Rotate(Vector3.forward, _zRotation);
    I've taken this from my code that rotates a bullet in the direction of the mouse when it gets shot, which is this portion, made with my (rather limited) knowledge of trig:
    Code (CSharp):
    1. Vector2 _mouseDir = Camera.main.ScreenToWorldPoint(_mouseScreenPos) - trans.position;
    2.         _mouseDir.Normalize();
    3.  
    4.         float _zRotation;
    5.         //in 1st or 4th quadrants
    6.         if (_mouseScreenPos.x >= Screen.width / 2)
    7.         {
    8.             _zRotation = Mathf.Asin(_mouseDir.y) * Mathf.Rad2Deg;
    9.         }
    10.  
    11.         else
    12.         {
    13.             //in 2nd quadrant
    14.             if (_mouseScreenPos.y >= Screen.height / 2)
    15.             {
    16.                 _zRotation = 90 - (Mathf.Asin(_mouseDir.y) * Mathf.Rad2Deg) + 90f;
    17.             }
    18.  
    19.             //in 3rd quadrant
    20.             else
    21.             {
    22.                 _zRotation = (Mathf.Asin(-_mouseDir.y) * Mathf.Rad2Deg) + 180f;
    23.             }
    24.         }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,311
    With 2d stuff you need to rotate around the axis going into the screen, so the z axis usually. Can easily do this by just getting the angle between some reference direction (your sprites normal orientation) and the direction to the player.

    Usually this reference direction is Vector2/3.up, and you can get the angle with Vector3.SignedAngle. You can make the rotation with
    Quatenion.AngleAxis
    , and just assign that to your game object.
     
  3. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    56
    My new code is:
    Code (CSharp):
    1. float _zRotation = Vector3.SignedAngle(attackAOE.transform.position - trans.position, player.position - trans.position, Vector3.up);
    2. attackAOE.transform.rotation = Quaternion.AngleAxis(_zRotation, Vector3.up);
    This doesn't work though as it rotates the attack AOE around the y-axis.

    So, I changed the code to this, using Vector3.forward:
    Code (CSharp):
    1.  
    2. float _zRotation = Vector3.SignedAngle(attackAOE.transform.position - trans.position, player.position - trans.position, Vector3.forward);
    3. ]attackAOE.transform.rotation = Quaternion.AngleAxis(_zRotation, Vector3.forward);
    But it still doesn't work correctly