I’m a complete beginner at unity so please bear with me. I have searched a lot and found some answers but I’m not fully understanding things.
The objective I want to achieve is to spawn a new AI character on the map. I then want that AI character to move toward a building on the map. However there could be multiple spawns of these AI characters and several other buildings I want them to move to independently to each other.
I have a couple of issues really. First, is the following a correct way to organise the code:
- I have an empty gameObject next to
the first building as a spawn
location for the AI. A script is also
attached to this empty gameObject to
control the spawning of the AI. - I have a second empty gameObject next
to another building for the AI to
move toward. - I have a prefab character as the AI
with a script attached. This script
will control the AI movement and
behaviours etc.
One issue here, is that I may also want the AI to spawn at a different building, so creating lots of empty gameObjects with scripts for spawning this AI on the other buildings seems like a stupid way to do it?
My other issue is that there is a disconnect between the spawn of the AI and the movement. So I am struggling to get the AI character to move to another location after spawning and not really understanding how to trigger this kind of action outside of the Update() function.
Sorry if this seems really dumb but any kind of advice would be appreciated.
Here is the spawn script:
#pragma strict
var spawnLocations : Transform[];
var aiPrefab : GameObject;
var aiPrefabClone : GameObject;
function Start(){
spawnAi();
}
function spawnAi(){
aiPrefabClone = Instantiate(aiPrefab,spawnLocations[0].transform.position,Quaternion.Euler(0,0,0)) as GameObject;
aiPrefabClone.goToMine(spawnLocations[1].transform.location);
}
Here is the script attached to the AI Prefab:
#pragma strict
function Start () {
}
function Update () {
}
function goToMine(location){
transform.position = Vector3.Lerp(transform.position, location, Time.time);
}
This is currently throwing an error:
BCE0019: ‘goToMine’ is not a member of ‘UnityEngine.GameObject’.
Thanks