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.
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.
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;
}
}