- Home /
help with gameObject.translate
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
Answer by Rahazan · Nov 13, 2013 at 03:08 AM
You are moving the spawnpoint, the enemyPosition pointer points to the Transform of the spawn because of
void Start () {
enemyPosition = GameMaster.spawnPoint;
}
You then move this transform around with the translation.
What you probably want to do is to make the enemy's position the position of the spawnpoint (and then move the enemy around).
You could see the Transform class as a box with some information in it (most importantly position, rotation and scale). With the code above you are making enemyPosition point to that box, and then in EnemyPosition() you changes the values inside that box. That is why your spawnpoint is moving (and with more enemies there are more enemies changing that same transform position, so it moves faster).
What you probably want to do is take information from the spawnpoint box (The Vector3 inside it) and copy it into the box that belongs to the enemy.
So basically said: When you work with a Vector3 (which is a struct) you could see it as a local copy, it works similar as a primitive (int, float, etc.). When you work with a Transform you are working with a pointer to some instance.
Now back to your code
What you do in your Start() method is probably not necessary, as you already instantiate the enemy on the spawnpoint in your GameMaster class.
For the pathfinding, instead, you might want to do is something like this:
public Transform target;
void Start () {
target = GameObject.FindGameObjectWithTag("Evil Player").transform;
}
And then in your EnemyPosition() function (which you should maybe rename to EnemyPositionUpdate()) you then move your enemy, maybe something like this:
public void EnemyPosition() {
Vector3 whereIWantToBe = target.position;
Vector3 direction = whereIWantToBe - transform.position;
transform.Translate(direction * Time.deltaTime);
}
Hope it helps!
Thank you very much for explaining. I think I understand how it works now.