So basically I have this script that would randomize the location of the GameObject every “spawnTime” and it is doing its job.
However, I thought the code meant that its transform.position would be adjusted again after it is within a distance of 4 from the gameObject “Player” However it seem that even though both this script and the script containing “OnTriggerEnter” on my “Player” gameObject were executed at the default time (not utilizing “Script Execution Order”,
the transform.position of this gameObject would be adjusted before taking into account the “OnTriggerEnter” of the “player” gameObject
I would like for someone to confirm what I wrote in bold above is true or explain why it is wrong. I thought that it would’ve run the command for “OnTriggerEnter” and changing the transform.position of this gameobjec at the same time.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Rotator : MonoBehaviour {
public float spawnTime;
public float startingSpawnTime;
public float distance;
public GameObject player;
private void Start()
{
distance = 4;
InvokeRepeating("Spawn", startingSpawnTime, spawnTime);
}
void Update ()
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
void Spawn()
{
do
{
transform.position = new Vector3(Random.Range(-9, 9), 0.5f, Random.Range(-9, 9));
} while ((player.transform.position - transform.position).magnitude < distance);
}
}