- Home /
Pause the enemy follow script after instantiate (2D, C#)
I have the following script that forces the "BabySpider" to chase after the player:
void FixedUpdate() {
//Use the following script to have an enemy run towards and attempt to touch the player
target = GameObject.FindWithTag("Player").transform;
Vector3 forwardAxis = new Vector3(0, 0, -1);
transform.LookAt(target.position, forwardAxis); //this makes the enemy face the direction that it is travelling
Debug.DrawLine(transform.position, target.position);
transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.z);
transform.position -= transform.TransformDirection(Vector2.up) * speed; //This equation determines the speed that the enemy is travelling
}
The are instantiated out of an "EggSack". I want them to pause for a second or two before they begin going after the player.
I tried searching for the terms you mentioned but I couldn't find a good answer. I may not be typing in the right key words.
Please click Tick, If answered correctly. SO that others can know the Question is answered.
Answer by GiyomuGames · Aug 12, 2015 at 05:50 AM
You can simply store the Time.time in the Start method of your script, then in your FixedUpdated you don't do anything until the actual time is greater than start time + 1 or 2 seconds.
1 additional tip, put this
target = GameObject.FindWithTag("Player").transform;
in your Start method. It is unnecessary to call it at every cycle.
$$anonymous$$ight as well tack on - why is OP using FixedUpdate(), there's no physics or Rigidbody involved here.
@getyou411 Because it gaurentees framerate independence. Unless you want to write "* Time.deltaTime" everywhere. Better use FixedUpdate unless the frequency of everything in the function will never ever influence the gameplay in any way.
Answer by Runalotski · Aug 12, 2015 at 10:27 AM
You can use a Corutine to delay functions as they wont go past the yield point until the specified time has passed wich is one second in this example in "IEnumerator SpiderHatching()"
Then we only want it to run once when the spider is Instantiated so we start the corutine in the start function
and then write the UpdateCode inside an if target != null so that if target is not set to anything yet then the spider will not move until it is given a target.
IEnumerator SpiderHatching()
{
//this code will wait for one second
yield return new WaitForSeconds(1f);
//and then set the player as target
target = GameObject.FindWithTag("Player").transform;
}
//now we want the start this when the spider is spawned
void Start()
{
StartCoroutine(SpiderHatching());
}
//now change you update to not run if target is Null
void Update()
{
if(target != null)
{
//I took this line out because we do not need it
//target = GameObject.FindWithTag("Player").transform;
Vector3 forwardAxis = new Vector3(0, 0, -1);
transform.LookAt(target.position, forwardAxis); //this makes the enemy face the direction that it is travelling
Debug.DrawLine(transform.position, target.position);
transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.z);
transform.position -= transform.TransformDirection(Vector2.up) * speed;
}
}
Your answer
Follow this Question
Related Questions
Instantiate an object in a prefab? 1 Answer
Checking if object intersects? 1 Answer
Instantiated objects at beginning of runtime are messing with the pathing for my NavMesh. 0 Answers
gameobject spawn in randomrange coordinates 1 Answer
Why is there a difference between script and in-game sequence of a script loop? 1 Answer