Search Unity

Resolved Rotate to the target (Canvas indicator)

Discussion in 'Physics' started by Geckoo, Jun 1, 2021.

  1. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    Hello. I use in a project a system which shows as Damage Indicator where are my enemies according my position. All works as expected, but there is a little problem because of an important feature - my spaceship uses a 6dof controller (like in Forsaken, Descent or OverLoad games). So it can rotate on each axis - and this way sometimes its head (main camera) is upside/down. Then Damage Indicators are inverted in my UI. I am trying to fix this problem adding a correction, but quaternions are so confusing for me. I use this function to display Damage Indicator, but it is efficient only for a simple FPS controller, not for a 6dof controller :

    Code (CSharp):
    1.  
    2. IEnumerator RotateToTheTarget()
    3. {
    4.     while (enabled)
    5.     {
    6.         if (Target)
    7.         {
    8.             tPos = Target.position;
    9.             tRot = Target.rotation;
    10.         }
    11.  
    12.         Vector3 direction = (player.position - tPos).normalized;
    13.  
    14.         tRot = Quaternion.LookRotation(direction);
    15.         tRot.z = -tRot.y;
    16.         tRot.x = 0;
    17.         tRot.y = 0;
    18.  
    19.         Vector3 northDirection = new Vector3(0, 0, player.eulerAngles.y);
    20.         GetComponent<RectTransform>().localRotation = tRot * Quaternion.Euler(northDirection);
    21.  
    22.         yield return null;
    23.     }
    24. }
    25.  
    On the first picture, the indicator works as expected :

    On the second, my ship is upside/down and indicator is inverted :


    There is something wrong with my NorthDirection definition. My Damage Indicator UI rotates correctly in a circle using an anchor, but when my ship is upside/down, all Damage Indicators are inverted. I guess that I should change something, but I tried many variants - it's worst. Have you an idea? Thank you for your help. Sorry for my poor English ++
     
    Last edited: Jun 1, 2021
  2. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    Hello. I have another idea. Is there a way to translate WorldToScreenPoint to radians - an indicator similar with a clockwise. This way an object in up/left in my camera has for radian ~=300 and down/right ~=120. I have the canvas UI, but I need a way so as to compute WorldToScreenPoint to radians. Thank you ++
     
  3. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    Hello. Finally I found a good solution. I totally changed my code for something more logical. I had some difficulties so that I understand some important things like difference between Vector3.forward and transform.forward. In the code below, I don't use Vector3.Angle because it is always relative. I hope that it could help someone ++

    Code (CSharp):
    1. public static float AngleSigned(Vector3 v1, Vector3 v2, Vector3 n)
    2. {
    3.      return Mathf.Atan2(Vector3.Dot(n, Vector3.Cross(v1, v2)), Vector3.Dot(v1, v2)) * Mathf.Rad2Deg;
    4. }
    5.  
    6. IEnumerator RotateToTheTarget()
    7. {
    8.     while (enabled)
    9.     {      
    10.         Vector3 direction = (player.position - tPos).normalized;
    11.         Vector3 correction = new Vector3(0, 0, -AngleSigned(player.forward, direction, player.up));
    12.  
    13.         GetComponent<RectTransform>().localRotation = Quaternion.Euler(correction);
    14.  
    15.         yield return null;
    16.     }
    17. }
     
    AshwinTheGammer likes this.
  4. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    Glad to see that you've successfully resolved this problem.
    Could you show me the damage indicator function in-game?
    Using gif or something?
    Could you also explain this line of code:

    Vector3 direction = (player.position - tPos).normalized;
    Vector3 correction = new Vector3(0, 0, -AngleSigned(player.forward, direction, player.up));


    What is tPos here?
    I'm also having some problem in making of damage indicator. It does work but doesnt show correct position of damage.
    Looking forward for your reply.
    :)
     
    Last edited: Jul 8, 2021