- Home /
How do i make the enemy go away from multiple enemies.
I can do it with one player but not two or more.
Answer by Enyph-Games · Jun 12, 2017 at 01:22 PM
You should post your code here.
Have you tried getting the direction away from one player, getting the direction away from the other player, and then finding the average direction or something like that? :D
Thanks for the help! I figured it out though here's what I have: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Person : $$anonymous$$onoBehaviour { GameObject[] gb; int translatecount = 0; // Use this for initialization void Start() { gb = GameObject.FindGameObjectsWithTag("Zombie"); }
private void Update()
{
transform.position = new Vector3(transform.position.x,-1.112969f,transform.position.z);
if (gb.Length != 0)
{
for (int i = 0; i <gb.Length; i++)
{
{
if (Vector3.Distance(transform.position, gb[i].transform.position) < 3)
{
Vector3 dir = gb[i].gameObject.transform.position.normalized * -1;
transform.Translate(dir * 1 * Time.deltaTime);
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Problem with Prefab 2 Answers
How do I specify the position of one object relative to another object? 2 Answers
2D Background Stop Moving on BoundingBox Exit? 2 Answers
How to make enemy`s gun shoot directly on player and its allies from some distance. 1 Answer
NavMeshAgent does not follow player 0 Answers