Unity - How to make A Chasing Enemy
HTML-код
- Опубликовано: 16 дек 2022
- Today we're making a simple bit of code that'll make an enemy chase after you, building off the previous video where we discussed the basics of working with a nevmesh
get the Project here : / unity-3d-75458669
Join the discord for any help you need! : / discord
support the channel and development over on patreon : / thegamingcaves Наука
I never thought it would be this easy, thank you very much. It helped a lot 🙏🏻
nice Tutorial. Thanks a lot
Glad you liked it!
Will this work for vr?
it should work for VR I think. i see no reason it shouldn't work at least.
I am trying to get a Hunter vs Prey AI set up and this has been best tutorial to expanded I have found.
I wanted the AI here to find multiple players or prey for me. Here are the changes I made to find multiple instead of just player or one gameobject.
public GameObject[] prey; //Line 8 for me
prey = GameObject.FindGameObjectsWithTag("PreyBull"); //Line 27 for me. The "PreyBull" is your tag so set name correct.
// to find the closest prey to chase and stick to it unless it gets away.
void Chase()
{
float minDist = Mathf.Infinity;
Vector3 nearest = Vector3.zero;
foreach (GameObject _prey in prey)
{
float dist = Vector3.Distance(transform.position, _prey.transform.position);
if (dist < minDist)
{
minDist = dist;
nearest = _prey.transform.position;
}
}
agent.SetDestination(nearest);
}