- Home /
Object following you?
I have a script:
function Update(){
var targ = GameObject("Player");
transform.position = Vector3.MoveTowards(transform.position, targ.transform.position, .03);
}
But this only works for 5 seconds. it will come up to your spawn then stop. any idea on how to get it to follow you like once every 30 sec.
Answer by Arithan · Jul 25, 2013 at 04:40 AM
I'm thinking it's stopping because it's creating an object named "Player" at some location and then the object is only following that reference to "Player". You also have that in Update() when means it's creating a GameObject named "Player" every frame.
Try something like this instead (untested):
var targ : GameObject;
function Start(){
targ = GameObject.Find("Player");
}
function Update(){
transform.position = Vector3.MoveTowards(transform.position, targ.transform.position, .03);
}
If your questions is answered, click on the checkmark next to the answer to close it out. Thanks.
Answer by FreeTimeDev · Jul 25, 2013 at 04:41 AM
Is your "targ" object the right object you want to follow?
Try something like: (Not tested, so fix any typos i might make!)
public var targ : Transform;
function Update()
{
transform.position = Vector3.MoveTowards(transform.position, targ.transform.position, .03);
}
so you don't needlessly repeatedly find the same object. Anyway, with this you have to drag and drop your player object onto the inspector to link it up but it'll work.
Your answer
Follow this Question
Related Questions
Simple Script Error? 1 Answer
It is not possible to invoke an expression of type 'UnityEngine.Vector3'. 1 Answer
2D Camera Smooth Follow with Dynamic Offset 0 Answers
Find Transform in the scene 2 Answers
Help with Enemy AI 1 Answer