Ulco
1
Hi,
Since I am new to unity and this forum, bear with me if I ask a stupid question or a question that is already answered somewhere else. Here it is:
What if I want to set multiple triggers for my gameobject (player), like, see an enemy from 1000 yards, use sniper rifle from 500 yards, use M4A1 from 200 yards, throw stone from 10 yards or punch from 0.5 yards?
Do I make 5 different collider spheres or is there a better way to do that, like one big sphere and then calculate the distance between player and enemy and use different scrips for that?
Again, I am fresh meat, so much to learn for me. sometimes it is easier to ask a question then try to find every question I have
, hope you understand
I think you only need one trigger, the biggest one (1000y) for seeing the target. And another small one (probably not trigger) for hitting the target. Since you know the distance (500y) for the rifle you can just check the squared distance to see if the target is in range.
Code for that is pretty simple:
if ((target.position - transform.position).sqrMagnitude <= rifleRange * rifleRange) {
//in range
}
else {
//not in range
}
This is faster to do for the computer than calculating the actual distance so you don’t lose performance. And since you don’t have so many colliders in the scene, the computer doesn’t have to check so many colliders against every other. You can just check the ones in range by iterating a list of enemies in range.
You can just add enemies to the list with OnTriggerEnter and remove with OnTriggerExit (or death).