Vector3 math help, creatures looking at gameobject.

My script worked perfectly with transform.position assuming the creature it’self did not move. If the creature moves, then the eye stays in place.

So i used transform.localPosition instead, and now the eye stays with the creature, but it’s not looking directly at the player.

If i throw in multiple creatures, they are all looking in a single direction, instead of all looking at the player.

You can see the problem here:

Here is the script:

public float lookRadius = 1; //Basically the size of the eyeball
	public float trackingSpeed = 1; //How fast the eyeball moves

	float elevation; //used to lock the eyeball into a plane, rather than moving in 3d space
	Vector3 targetPosition; //The target the eyeball is tracking
	Vector3 centerPos; // The center of the eyeball

	// Use this for initialization
	void Start () {

		centerPos = transform.localPosition;
	void Update () {
		eyeballTracking ();

	//Track movement of another object while being confined to a radius
	void eyeballTracking () {

		elevation = transform.position.y;

		targetPosition = new Vector3(trackMe.playerPosX, elevation, trackMe.playerPosZ);
		Vector3 offset = targetPosition - centerPos;
		transform.localPosition = centerPos + Vector3.ClampMagnitude(offset, lookRadius) * trackingSpeed * Time.deltaTime;



transform.localPosition += centerPos + Vector3.ClampMagnitude(offset, lookRadius) * trackingSpeed * Time.deltaTime

It looks to me like you need to recalculate centerPos. You only do it once at startup, so if your eye moves, it is still using the previously calculated ‘centerPos’. So put this in the top of ‘eyeballTracking()’:

centerPos = transform.localPosition;

So i actually solved this by side stepping it completely, i made the eye onto two parts, the parent was the eyeall that has a “look at” function, and then the black part of the eye is a child where i locked it’s rotation so it will move around in a circle, but stay at a fixed rotation. It is much simpler this way.

Thanks for all the suggestions.