- Home /
Transform Position?
Is there any way to store a transform position in a prefab, so that when the object is spawned or you put it in a different scene, it always follows the same object? This is the script:
EDIT- I attempted to make it follow the tagged object but I get errors.
using UnityEngine; using System.Collections;
public class BStandard : MonoBehaviour { //public Transform playerPos; Vector3 chaseVel, chaseDir, chasPos;
void Start() { chaseVel = new Vector3(10f, 0f, 10f);
playerPos = GameObject.FindWithTag("Player");}
void LateUpdate()
{
chaseDir = playerPos.position - transform.position;
Vector3 tmpNormalizedDir = Vector3.Normalize(chaseDir);
chaseDir = new Vector3(tmpNormalizedDir.x * chaseVel.x, tmpNormalizedDir.y * chaseVel.y, tmpNormalizedDir.z * chaseVel.z);
chaseDir *= Time.deltaTime;
this.transform.position += chaseDir;
}
}
The_r0nin was right, but you're doing it wrong. You are setting playerPos equal to the player's game object, so your trying to access position of a gameobject, which doesn't exist. on the first line of your LateUpdate(), change playerPos.position to playerPos.transform.position
Answer by The_r0nin · Dec 01, 2010 at 04:29 PM
Give your object to be followed a tag (or put a tag on an empty child object of it). Then in Awake() do a GameObject.FindWithTag and store this in your variable for the object to be followed...
I tried to do that (the new script is in my edited question), but I get errors.