Help about damage indicator

Hi to all i need some help with my damage indicator script i’m using in my prototype template…

pratically the indicator seems to not set correct orgiin attack direction as you can see in the jpg

here the code

using UnityEngine;
using System.Collections;
 
public class DamageIndicator : MonoBehaviour {
 
    /// <summary>
    /// Attack from direction
    /// </summary>
    [HideInInspector]
    public Vector3 attackDirection;
    /// <summary>
    /// time reach for fade arrow
    /// </summary>
    public float FadeTime = 3;
    public Texture damageIndicatorTexture;
    public Color ArrowColor = new Color(0.85f, 0, 0);
    /// <summary>
    /// the transform root of player
    /// </summary>
    public Transform target;
    //Private
    public Vector2 pivotPoint;
    public float alpha = 0.0f;
    public float offset = 350f;
    public float rotationOffset;
 
    /// <summary>
    ///
    /// </summary>
    void OnGUI()
    {
        if (this.alpha > 0)
        {
            this.offset = Screen.height / 4;
            GUI.color = new Color(ArrowColor.r, ArrowColor.g, ArrowColor.b, this.alpha);
            Vector2 vector = new Vector2(((Screen.width / 2) - (this.damageIndicatorTexture.width / 2)), (Screen.height / 2) - this.offset);
            this.pivotPoint = new Vector2((Screen.width / 2), (Screen.height / 2));
            GUIUtility.RotateAroundPivot(this.rotationOffset, this.pivotPoint);
            GUI.DrawTexture(new Rect(vector.x, vector.y, this.damageIndicatorTexture.width, this.damageIndicatorTexture.height), this.damageIndicatorTexture);
        }
    }
 
    /// <summary>
    /// Use this to send a new direction of attack
    /// </summary>
    /// <param name="dir">postion of attacker</param>
    public void AttackFrom(Vector3 dir)
    {
        //target = this.transform ;
        this.attackDirection = dir;
        this.alpha = 3f;
    }
    /// <summary>
    /// if this is visible Update position
    /// </summary>
    void Update()
    {
        if (this.alpha > 0)
        {
            this.alpha -= Time.deltaTime;
            this.UpdateDirection();
        }
    }
    /// <summary>
    /// update direction as the arrow shows
    /// </summary>
    void UpdateDirection()
    {
        Vector3 rhs = this.attackDirection - this.target.position;
        rhs.y = 0;
        rhs.Normalize();
        Vector3 forward;
        if (Camera.main != null)
        {
            forward = Camera.main.transform.forward;
        }
        else
        {
            if (Camera.current != null)
            {
                forward = Camera.current.transform.forward;
            }
            else
            {
                forward = this.transform.forward;
            }
        }
        float GetPos = Vector3.Dot(forward, rhs);
        if (Vector3.Cross(forward, rhs).y > 0)
        {
            this.rotationOffset = (1f - GetPos) * 90;
        }
        else
        {
            this.rotationOffset = (1f - GetPos) * -90;
        }
    }
}

@JC_LEON

Edit: formatted the whole answer - hopefully it is more understandable.

You could try the following method I cut’n’pasted here.

It will give an angle, which is relative to player forward… not based on world directions.

I’ve also flattened the vectors to “map plane” so that the angles won’t get messed up if either player or enemy is uphill / downhill relative to each other.

Using “useDirection”, you can decide, what you use to depict direction; if true, hit indicator angle is taken from direction of bullet’s vector (or whatever it is). When false, direction is derived from other object’s relative position to player.

Both approaches use the same method to get angle, that you can then feed to your direction indicator. See how the other transform position is made usable in GetHitAngle method call.

Note: Update methods if/else and useDirection bool are just for a demo, you don’t probably need optional things there in practice.

public RectTransform indicator;
public Transform otherTransform;
public Transform player;
public bool useDirection;
float angle = 0;

void Update () 
{
	if (useDirection)
	{
		// Angle based on the direction of the shooter relative to player
		angle = GetHitAngle(player, otherTransform.forward);
		indicator.rotation = Quaternion.Euler(0,0, -angle);
	}
	else if (!useDirection)
	{
		// Angle taken from other objects pos and player
		angle =GetHitAngle(player, (player.position - otherTransform.position).normalized);
		indicator.rotation = Quaternion.Euler(0,0, -angle);
	}
}
    
public float GetHitAngle (Transform player, Vector3 incomingDir)
{
	// Flatten to plane
	var otherDir = new Vector3(-incomingDir.x, 0f, -incomingDir.z);
	var playerFwd = Vector3.ProjectOnPlane(player.forward, Vector3.up);

	// Direction between player fwd and incoming object
	var angle = Vector3.SignedAngle(playerFwd, otherDir, Vector3.up);
	
	return angle;
}

P.S.

I wouldn’t use OnGUI, it is better to create UI canvas and add an Image component.

sincerly i did not really understood what you said…LOL

many thanks for you reply and explaination but ain my situation i dont know the orgin of hit so i think your suggestion cant be applied to my situation

many thanks again… sincerly i cannot figure the system you are trying to set for me
i really dont know ho to correcly set up the part of code you wrote for me and i’m not able t ocorreclty understand your reply… and to correcly call the method when needed…