I programed my enemies to look at me all the time using transform.LookAt. it worked fine, so I wanted to make them into a prefab to code them to spawn repeatedly. now, I cant add anything to the target variable (which is set up as target : Transform) that isnt a prefab. so I proceeded to make my character into a prefab. now, everything works fine, but the enemies look, at where my character spawns at the start of the game instead of constantly correcting to look at the new position. any ideas?
Move the LookAt into Update().
Because they’re looking at the prefab’s location, which doesn’t move. The prefab does not move.
You need to assign the player to the spawned prefabs in Start. For example:
function Start(){
target = GameObject.FindWithTag("Player").transform;
}
No I get an error saying target is an unknown identifier.
var moveSpeed : float;
function Start(){
target = GameObject.FindWithTag("Player").transform;
}
function FixedUpdate () {
rigidbody.AddRelativeForce(0, 0, moveSpeed);
}
function Update () {
Transform.LookAt(target);
if(transform.rotation.x > 0){
transform.rotation.x = 0;
}
if(transform.rotation.z > 0){
transform.rotation.z = 0;
}
}
Whats wrong now?
Does your player object have a “Player” tag?
Have you handled any and all ‘null’ situations?
What LOC is the error?
I added the tag. What’s an LOC?