- Home /
How to get an enemy to go to a location and then return.
I'm creating a 2D top-down game and I want a certain type of enemy to come out of a hole to attack the player and then return when they miss the player. (The enemies are one shot killers.) I have a basic idea for what I want to happen, but I don't know how to code it. I want the enemy to come out of hiding when the player reaches a certain area around it, head in the player's direction, and then smoothly return to it's hiding spot. I can't find anything for coding this type of behavior.,
I will give you a rough idea:-
1) Create a collider with isTrigger enabled near your cave. Attach a script to this collider. In this script use OnTriggerEnter
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider player)
{
//the player has entered this collider
// activate the $$anonymous$$onster inside this cave to run towards enemy's position
}
}
2) When the trigger is enabled, send an even to the respective $$anonymous$$onster to move towards enemy player [you can check Unity's Ethan tutorial which is very helpful and you can find something of relevance]
Answer by tormentoarmagedoom · Jul 18, 2018 at 12:37 PM
Good day.
You are asking different things. So go step by step. Learn to do this things:
Create an object during runtime (Instantiate())
Move an Object to specific direction
Detect when 2 objects are closer than X distance
Make an object decide what to do depending on some variables (like the distance)
Create a bullet
Make the bullet to move to target
Make the bullet to kill or take health from the target
Destroy an object (die)
STEP BY STEP!
Once you control all of this, you only need to compile all togather in the correct order and configuration!
Good luck!