Search Unity

Question Damage Indicator indicates Incorrect position.

Discussion in 'Scripting' started by AshwinTheGammer, Jul 8, 2021.

  1. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    Damage Indicator is indicating wrong position of the attack. You can see in gif. https://gyazo.com/6ee3b29dfbf813e5a910d1cda4687770

    I'm finding the angle using CrossProduct. I even tried SignedAngle but it didn't work.
    Code (CSharp):
    1.  private void Update(){  
    2.         for (int i = 0; i < indicatorList.Count; i++) //List type-indicatorList - max indicator allowed is 5
    3.         {
    4.             if (indicatorList[i].indicatorAlpha > 0)
    5.             {
    6.                 CalculateDamageIndicatorAngle(indicatorList[i]);
    7.                 indicatorList[i].indicatorAlpha -= Time.deltaTime; //Reduce the alpha
    8.             }
    9.             else
    10.             {
    11.                 indicatorList.RemoveAt(i);
    12.             }
    13.         }
    14. }
    15.  
    16. for (int i = 0; i < indicatorList.Count; i++)
    17.         {
    18. //hitIndicatorUI-Image Type...Contains Image of indicator
    19.           hitIndicatorUI.color = new Color(hitIndicatorUI.color.r, 0f, 0f, indicatorList[i].indicatorAlpha);
    20.  
    21.           hitIndicatorUI.rectTransform.rotation = Quaternion.Euler(0f, 0f, indicatorList[i].indicatorAngle);
    22.           hitIndicatorUI.rectTransform.rotation = Quaternion.Euler(0f, 0f, 0-indicatorList[i].indicatorAngle);
    23.         }
    24.         hitIndicatorUI.color = new Color(hitIndicatorUI.color.r, hitIndicatorUI.color.g, hitIndicatorUI.color.b, 1f);
    25.  
    26. private void CalculateDamageIndicatorAngle(DamageIndicator d)
    27.     {
    28.         Vector3 lhs = playerCamera.transform.forward;
    29.  
    30.         Vector3 rhs = d.damageDir - transform.position;
    31.         rhs.y = 0;
    32.         rhs.Normalize();
    33.          d.indicatorAngle = Vector3.Cross(lhs, rhs).y > 0 ? (1 - Vector3.Dot(lhs, rhs)) * 90 : (1 - Vector3.Dot(lhs, rhs)) * -90;
    34.         //d.indicatorAngle = Vector3.SignedAngle(lhs, rhs,Vector3.down);
    35.     }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Instead of cross product, try using atan2, that's its job.
     
    AshwinTheGammer likes this.
  3. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    Well, I'm not very good in maths :p But here's what I tried..
    Its still not working.

    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.     private void CalculateDamageIndicatorAngle(DamageIndicator d)
    6.     {
    7.         Vector3 lhs = playerCamera.transform.forward;
    8.  
    9.         Vector3 rhs = d.damageDir - transform.position;
    10.        // rhs.y = 0;
    11.         rhs.Normalize();
    12.         //d.indicatorAngle = Vector3.Cross(lhs, rhs).y > 0 ? (1 - Vector3.Dot(lhs, rhs)) * 90 : (1 - Vector3.Dot(lhs, rhs)) * -90;
    13.         d.indicatorAngle = AngleSigned(lhs, rhs, playerCamera.transform.up) ;
    14.     }
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Does it have like a constant offset from where you are or anything? Or is it just basically random?
     
    AshwinTheGammer likes this.
  5. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    I don't think that it have constant offset from where player is.
    Yeah u can say it's basically random.
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Try logging out the angle, see if that makes sense. Maybe you expect angles and get radians or vica versa?
     
    AshwinTheGammer likes this.
  7. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    d.indicatorAngle is float type
    I guess something is wrong in calculating angle....dunno what?:confused:
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    ? I don't understand, did you debug log it out?
     
    AshwinTheGammer likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    As @gorbit99 has suggested, nobody here knows what either. You have to find out what and the easiest way is to use Debug.Log().

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    AshwinTheGammer likes this.