- Home /
Objects not following
So, firstly, here's my code:
var target : GameObject;
var range : float;
var speed : float;
function Update ()
{
range = Vector3.Distance(target.transform.position, transform.position);
if (range<50)
{
transform.LookAt(target.transform.position);
transform.Translate(Vector3.forward*speed*5);
}
}
The objects are cubes and the target should be the player. The cubes are instantiated randomly, and it doesn't let me set the first person controller as the target in the prefab. I tried making the player a prefab and then using that as the target, but that didn't work. Any help?
Answer by GesterX · May 12, 2011 at 12:03 PM
Tag your player as "Player" then do something like this:
function Start()
{
target = GameObject.FindWithTag("Player");
}
This will make the cube set its target as the GameObject tagged as "Player" as soon as it spawns.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133702im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate a prefab and follow player position in runtime? 0 Answers
Camera rotation around player while following. 6 Answers
Instantiate prefab from original position to click mouse point position 0 Answers
Semi-Quick question about "LookAt" script after respawn. 1 Answer
player prefab messing up the scores 1 Answer