Trying to write this code on my own but I can’t get my spawned game objects to move, so here I am! Lost as can be.
I have 4 game object spawn points, each spawn point has a box collider with “is trigger” checked. My enemies spawn, but they don’t move? Will it cause an issue if the enemies have rigid bodies on them?
code
using UnityEngine;
public class SpawnEnemies : MonoBehaviour
{
public GameObject enemy;
public Transform moveSpawn;
public float spawnTime = 3.0f;
public float speed = 1.0f;
public Transform[] spawnPoints;
bool isCollided = false;
void Start ()
{
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}
void Spawn()
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Spawn")
{
isCollided = true;
}
}
private void Update()
{
if (isCollided == true)
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, moveSpawn.position, step);
}
}
}
Pengocat is correct.
However, you may have another issue (probably). One which game object is this script located?
You are attempting to move whichever object this script is attached to, by using : transform.position & movetowards
It’s more likely that you want to have the spawned entity be moving (and this script probably isn’t on that).
Script is located on the spawn points (AKA empty game objects) which also have the colliders on them. My enemies have a rigid body.
So using the transform.position movetowards that has to be on the object that I’m moving? So essentially I should make two scripts? A spawn and move script?
Indeed. the ‘transform’ is getting the current gameobject, from any script it’s called on. You can , of course, access other transforms the script is aware of… but in your situation, you’d want a separate script, as I understand it. You want them to spawn at one point, and move towards another point?
Object transform is where you want the prefabs to go once instantiated? If that’s the case, if your new script is called “Enemies”, maybe make a variable for ‘destination’
When you instantiate them, you could do:
//
//somewhere in Enemies:
public Vector3 destination;
// Inside Spawn enemies:
//where you're creating them
GameObject newEnemy = Instantiate (/*your code*/);
newEnemy.GetComponent<Enemies>().destination = spawnPoint; // or whatever it's called :)
I’m a bit sleepy , but is that what you’re asking about?
If you want the enemy to spawn somewhere, and walk to a specific point, I think this will work out well for you. The enemies script updates the movement until it arrives (… and then you do whatever you’re doing with the rest of your game! :))
I should clarify that you could have used the enemy’s transform from within the other script, technically speaking. However, in your situation with your “isCollided” (that variable would have made sense only once for the first collision, etc)… It seemed a lot simpler to explain this other option to you. While ‘transform.position’ affects only the gameobject, you can reference any other gameobject (or component of one, ie: transform/script/anything), then access its ‘transform’ component and from there, modify/use it… lol too much information?