I have a spawn script which does work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
GameObject myCharacter = null;
GameObject myCharacterSpawnPoint = null;
Transform myCharacterTransform;
// Use this for initialization
void Start()
{
// spawn the character
myCharacterTransform.position = Vector3(0.0f, 0.0f, 0.0f);
for (int i = 0; i < 2; i++)
{
// add a little randomness to spawning
Vector3 tempPos = myCharacterSpawnPoint.transform.position;
tempPos.x += Random.Range(2, 4);
tempPos.z += Random.Range(2, 4);
myCharacterSpawnPoint.transform.position = tempPos;
myCharacter = Instantiate(Resources.Load("Characters\\MyCharacter"), grannyTransform.position, Quaternion.identity) as GameObject;
myCharacter .name = "MyCharacter" + i;
}
}
}
I also have a GotoWayPoint script that is attached to my character in resources:
If I have one character spawn in the for loop everything works.
If I have more that one character spawn in the for loop only the last character returns to the idle position.
All characters stop moving (but keeps animating except for the one) which is normal when they reach the waypoint.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GotoWayPoint : MonoBehaviour
{
private Transform targetWayPoint.position = Vector3(10.0f, 0.0f, 10.0f);
public bool isIdle;
public bool isWalking;
public float positionSpeed = 10;
public float rotationSpeed = 2;
// control the animation
static Animator animator;
private Transform characterTransform;
// Use this for initialization
void Start()
{
// get a reference to the animator
animator = GetComponent<Animator>();
animator.SetBool("isWalking", isWalking);
animator.SetBool("isIdle", isIdle);
}
// Update is called once per frame
void Update()
{
// rotate towards the target
Vector3 targetDir = (targetWayPoint.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(targetDir) * Quaternion.Euler(0, rotationOffset, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
// move towards the target
transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position, positionSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, targetWayPoint.position) < 1)
{
Debug.Log(this.name + " Stop*******************************");
// reached the waypoint
animator.SetBool("isWalking", false);
animator.SetBool("isIdle", true);
}
}
}