- Home /
how to make enemy go around the other enemy to attack player,not just move,then keep push enemy.
public class blueMonster : MonoBehaviour
{
[SerializeField] private float detect, speed, attackR, setTime;
private Vector3 enemy;
private Vector2 sta, end;
private Animator animator;
private RaycastHit2D hit;
private float mv, dis, noMove;
public MonsterState currentState;
private List<GameObject> k;
void Start()
{
currentState = MonsterState.Idle;
k = new List<GameObject>();
animator = GetComponent<Animator>();
mv = -1f;
speed = 1.5f;
attackR = 1.5f;
setTime = 1f;
detect = transform.Find("detectRange").gameObject.GetComponent<CircleCollider2D>().radius;
dis = detect;
animator.SetFloat("moveX", mv);
StartCoroutine(waitIdle(setTime));
}
private IEnumerator waitIdle(float t)
{
yield return new WaitForSeconds(t);
currentState = MonsterState.walk;
animator.SetBool("moving", true);
}
void Update()
{
monsterBehavior();
}
void monsterBehavior()
{
if (dis <= attackR && currentState != MonsterState.attack)
{
animator.SetBool("moving", false);
StartCoroutine(AttackCo());
}
else if (currentState == MonsterState.track)
{
Vector2 direction = enemy - transform.position;
transform.position = Vector3.MoveTowards(transform.position, enemy, Time.deltaTime * speed); //移動
animator.SetFloat("moveX", direction.x);
animator.SetFloat("moveY", direction.y);
animator.SetBool("moving", true);
}
else if (currentState == MonsterState.walk)
{
transform.position += new Vector3(-(speed), 0, 0) * Time.deltaTime; //往前走
animator.SetFloat("moveX", mv);
animator.SetFloat("moveY", 0);
animator.SetBool("moving", true);
}
}
private IEnumerator AttackCo()
{
animator.SetBool("attacking", true);
currentState = MonsterState.attack;
yield return null;
animator.SetBool("attacking", false);
yield return new WaitForSeconds(1f);
currentState = MonsterState.track;
}
private void OnTriggerStay2D(Collider2D other)
{
if (other.CompareTag("red"))
{
if (k.Count == 0)
{
k.Add(other.gameObject);
}
else if (other.gameObject == k[0])
{
currentState = MonsterState.track;
enemy = other.transform.position;
dis = Vector3.Distance(transform.position, enemy); //兩座標之間的距離長度
}
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("red"))
{
if (k.Count > 0 && other.gameObject == k[0])
{
k.Remove(other.gameObject);
dis = detect;
currentState = MonsterState.walk;
}
}
}
}
Tell me good solutation,I really need to help.PLEASE~~~!!!
Answer by yuvaansh · Aug 31, 2021 at 12:22 PM
@awdrg052
You can use Raycast to determine if an enemy is standing in Front of the other enemy and if it is then move to either left or right & you should also add code in which after the enemy has moved right/left then it should again do an Raycast to see if any other enemy is standing in front of the current enemy. I hope that made sense & answered your question
;-D
how do I use the Raycast ? I had search this but I still no how to use.
how do I know the other Enemy move to left or to right ,trigger or something else?
@awdrg052
Sorry for the late reply. But you should use Raycast like this.
RaycastHit object;
if (Physics.Raycast(enemy.transform.position, enemy.transform.forward, out object, range) && object.transform.tag == "Enemy") {
//Move left/right code
}
Also a couple more things that I think you have this script on the Enemy prefab (if you have prefab). Second thing is that you should add a tag to the Enemy prefab named whatever you want for reference I have written Enemy in the script & you can also use "this.gameobject .transform" in the script but just make things short i just wrote enemy. Last that this should be in the Enemy script. I hope this helped you :D
I use your method but still not working.I don't understand why.
public class NewBehaviourScript : MonoBehaviour
{
public float speed, max = 10;
RaycastHit k;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.DrawLine(transform.position, transform.forward, Color.black);
if (Physics.Raycast(transform.position, transform.forward, out k, max) && k.transform.tag=="enemy")
{
**Debug.Log("test");**
}
transform.position += new Vector3(-(speed), 0, 0) * Time.deltaTime;
}
}
Answer by Riiich · Aug 31, 2021 at 06:14 PM
You'll want to use some kind of pathfinding. My recommendation is the free A* Pathfinding project for Unity: https://arongranberg.com/astar/