Cam move and coordinates problems

Simple cube with cam in lookAt mode. Cube is moving only in X plane right and left ( GetAxis func ).
Now, if X value of cube is larger then +3.0f than Cam start to move after ( like following him ).
We are changing two things: x positions of cube and cam, and cam rotation which is cont ( lookAt )

The problem is that when camera is starting following, the rotation isn’t const but is smaller every frame … this causes that cam every frame is further from cube.

Debug shows difference X value of cube and Cam which in fact shoud be the same … but it isn’t.

public class move : MonoBehaviour {
	
	public Transform Cam;
	public float xPos;
	void Start () {
	
	}
	
	void LateUpdate()
	{
		Cam.LookAt(this.transform.position);
		if((this.transform.position.x - 3.0f) > Cam.transform.position.x)
		{
			Cam.transform.Translate(xPos, 0, Cam.rotation.y*xPos);
		}
		
		Debug.Log(this.transform.position.x - Cam.transform.position.x);
	}
	
	void Update () {
		xPos = Input.GetAxis ("Horizontal") * 0.08f;
		this.transform.Translate(xPos,0,0);
	}
}

Help ? :slight_smile:

Hi DeeCann,

your test is bad :

(this.transform.position.x - 3.0f) > Cam.transform.position.x)

you must use abs value like that :

Mathf.Abs(transform.position.x - Cam.transform.position.x) > 3

Julien G.