- Home /
Target Priority
Hello everyone. I'm currently working on a dungeon crawler game and I have a level part that is going to be a wave of enemies prioritizing the attack of an object istead of the player. I wanted to create something like a random number, that way thy would attack the object more than the player. Anyone can help me to how I can create a target priority system? Thank you.
Answer by highpockets · Mar 12, 2021 at 12:50 PM
I think you can maybe come up with a functionality that creates a min distance for the enemy to attack the player, but as the enemies spawn, make that distance a random float:
public class Enemy : MonoBehaviour
{
private float randomDist;
private Transform player;
private void Start()
{
randomDist = Random.Range(4.0f, 20.0f);
player = GameObject.FindWithTag(“Player”);
}
private void Update()
{
if(Vector3.Distance(transform.position, player.position) <= randomDist)
{
//Attack player
}
else
//Look for other opponents to attack
}
}
I thought about that the thing is, the player rich an area and only then the spawn point of the wave set active, if the player is too close to those point the enemy will prioritize the player instead of the object. What I want is more like tower defence, the enemies attack that object but there's a small probability of them attacking the player
Ok, well then you would have to create some functionality where if third party enemy is alive and within target range attack player by doing another random range. That random range can be 0-100 and if the returned number is 0-10 and you set the player attack probability to10 percent, then attack the player. It’s a similar thing really, but I don’t think you need to make it more complicated than that.
float totalAttackFactor = 100;
float attackPlayerProbability = 0.1f; //10 percent
if(Random.Range(0,totalAttackFactor) <= attackPlayerProbability * totalAttackFactor)
{
//Attack player
}
else
{
//Attack third party
}
Your answer
Follow this Question
Related Questions
Draw Calls increased after I upgraded to 4.0 1 Answer
Unity 4 drag and drop no longer works? 1 Answer
Three ingame buttons conflict with each other on iOS 0 Answers
Why does my trigger-collider receive Enter but not Stay or Exit? 1 Answer
Edit font texture (unity 4) for adding special effects. 2 Answers