Hi
I’m pretty new to this so it’s probably some really obvious mistake that I can’t find.
I’m making a simple tower defense game and for testing purpose I’m trying to spawn a new enemy every time I’m pressing the space bar. It works and a new clone of my enemy is created every time I press it. Then I tried to make the enemies move by attaching this pathfinder script to them(It is supposed to become one later). My problem is that it moves the spawn point and the enemies spawns on top of each other and moving exactly the same. I want the spawn point to be in the same position and the enemies to spawn there and then start to move. Here are my two scripts. the GameMaster.cs is attached to an empty gameobject and the PathFinding.cs is attached to my enemy prefab.
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public GameObject enemy;
public static Transform spawnPoint;
// Use this for initialization
void Start () {
spawnPoint = GameObject.FindGameObjectWithTag("Spawn Point").transform;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("space"))
SpawnEnemies();
}
public void SpawnEnemies() {
Debug.Log("Enemies spawned!");
GameObject.Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
}
}
using UnityEngine;
using System.Collections;
public class PathFinding : MonoBehaviour {
public Transform enemyPosition;
// Use this for initialization
void Start () {
enemyPosition = GameMaster.spawnPoint;
}
// Update is called once per frame
void Update () {
EnemyPosition();
}
public void EnemyPosition() {
enemyPosition.Translate(enemyPosition.forward * Time.deltaTime);
gameObject.transform.position = enemyPosition.position;
}
}
One more thing: If I press the spacebar multiple times in a row the speed of the enemies increase.
Thank you in advance