- Home /
Enemy Indicator
Hey Guys,
I want to show the Player in which direction is an enemy - an Indicator for each Enemy. So first i make an Array with all enemies. Now i make a RaycastHit from the Player to the Enemy. Around the Camera I have a rectangle. So then I'm Instantiate a Prefab, the arrow, and the position is the hit point of the RaycastHit with the rectangle. Everytime I'm update the indicator, the Array of enemies is cleared and the algorithm starts again.
But it don't work perfectly the arrow is not on the hit point is further away.
Here is the code:
private void GetEnemyTriangle()
{
Enemies = null;
foreach (GameObject indicator in GameObject.FindGameObjectsWithTag("Indicator"))
{
Destroy(indicator);
}
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject Enemy in Enemies)
{
RaycastHit hit = new RaycastHit();
if (Physics.Raycast( Enemy.transform.position,Player.transform.position, out hit))
{
GameObject NewIndicator = Instantiate(Arrow) as GameObject;
Quaternion indicatorRotation = Quaternion.LookRotation(Player.transform.position - Enemy.transform.position, Vector3.forward);
indicatorRotation.x = 0;
indicatorRotation.y = 0;
Vector2 pos = hit.point;
NewIndicator.tag = "Indicator";
NewIndicator.transform.rotation = indicatorRotation;
NewIndicator.transform.position = pos;
}
}
}
And a picture:
In the picture we see the blue indicators, the white lines are the RaycastHit and in the bottom we also see the rectangle
I hope you can help me to solve the problem :-)
Comment