How to find find closest enemy in a direction (Unity Tutorial)

How to find find closest enemy in a direction (Unity Tutorial)

ItsJustOneGuy

5 лет назад

2,819 Просмотров

Hey if you like the content please like and subscribe.
And if you want to see a specific tutorial comment down below
Thanks for watching

Just a simple script to find the closest enemy in the forward facing direction.

Anyway here are my social please like and sub and all the rest of that social media nonsense and thank you for watching the video. Every view helps.
—---------------------------------------Socials—---------------------------------------------
Instagram
https://www.instagram.com/itjustoneguy
Twitter
https://twitter.com/itsjustoneguy
Email
[email protected]
Facebook
https://www.facebook.com/Itjustoneguy-103618309123081
YouTube
https://www.youtube.com/c/Itsjustoneguy

–----------------------------------------Games I Made—---------------------------------
Some early games I made. Not my best work though lol

Evolve Die Repeat
https://itsjustoneguy.itch.io/evolvedierepeat
Mistborn Fan Made Game
https://itsjustoneguy.itch.io/unofficial-mistborn-fanmade-game

Тэги:

#unity #tutorial #find #closest #enemy #video #game #dev
Ссылки и html тэги не поддерживаются


Комментарии:

@technofreak4573
@technofreak4573 - 26.12.2021 14:16

Hey Man, Thanks for the code. It is detecting the players but for some reason it's not showing the closest enemy.

Ответить
@electrocreative4303
@electrocreative4303 - 20.01.2021 04:16

Thank you , after a long trip of research God Bless You

Ответить
@kaiser6594
@kaiser6594 - 20.01.2021 01:13

I didn't know what to do, thank you!!

Ответить
@djaz2928
@djaz2928 - 14.01.2021 21:07

Thank you. With some little modifications, You unblocked my issue

Ответить
@dailypistonuploads8364
@dailypistonuploads8364 - 03.12.2020 08:54

Thank you so much for this!

Ответить
@snisel9182
@snisel9182 - 26.09.2020 18:54

For some reason, it doesn't detect the enemies in the collider. I think I wrote everything perfectly but it still doesn't work.
This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrackEnemies : MonoBehaviour
{
public List<GameObject> enemies = new List<GameObject>(); //The enemies player detection collider is touching
public GameObject closestEnemyInDirection; // The closest enemy in the direction the player is facing
public Transform body; //The body the player using to find as the player

private void Start()
{

}

public void FindClosestGameObject()
{
GameObject closest = null;
float distance = 41.0f;
Vector3 position = body.transform.position;
foreach (GameObject go in enemies)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
closestEnemyInDirection = closest;
}

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
for (int i = 0; i < enemies.Count; i++)
{
if (other.gameObject == enemies[i])
{
return;
}
}
enemies.Add(other.gameObject);
FindClosestGameObject();
Debug.Log("ye");
}
}

private void OnTriggerExit(Collider other)
{
if(other.tag == "Enemy")
{
for (int i = 0; i < enemies.Count; i++)
{
if (other.gameObject == enemies[i])
{
if(enemies[i] == closestEnemyInDirection)
{
closestEnemyInDirection = null;
}
enemies.RemoveAt(i);
}
}
}
}
}

Ответить
@unicode3402
@unicode3402 - 11.09.2020 07:30

This is actually awesome, and for some reason really hard to find. Most tutorials focus on the closes enemy period, but games usually allow focus to closes enemies on a specific direction (hack and slashes, or detecting closest points based on camera angle...). Thanks for the code!

Ответить
@daoviettuan2002
@daoviettuan2002 - 13.06.2020 16:27

Like

Ответить