Search Unity

Damage direction indicator on screen.

Discussion in 'Scripting' started by O-R-C, Feb 14, 2016.

  1. O-R-C

    O-R-C

    Joined:
    Feb 16, 2014
    Posts:
    1
    I am trying to make an indication on the screen showing the direction of incoming damage.

    Finding the direction of the incoming damage is not a problem.

    Code (CSharp):
    1.     public Vector3 DamageDirection(Vector3 targetLocation, Vector3 damagerLocation)
    2.     {
    3.         var heading = damagerLocation - targetLocation;
    4.         heading.z = 0;
    5.         var distance = heading.magnitude;
    6.         var direction = heading / distance;
    7.         return direction;
    8.     }
    This is where I am having problems.
    So I have a UI image on the screen with the anchor in the middle of the screen.
    I have no idea on what maths I need to do to change the UI images rotation.z to make it point in the direction of the damage.
    I have looked through many other threads on this matter, but non of them have been what I have been looking for.

    I think the main problem I am facing is converting a vector 3 to a Quaternion.
    Any help would be appreciated.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    So far this has worked for me for rotating objects in general:
    Code (CSharp):
    1. transform.rotation = Quaternion.LookRotation(forward, up);
    forward is where the blue axis should point, and up is where the green axis should point.