# Camera movement and threshold not working

I have a platformer that is in progress, part of this has a camera which I only want to move when the character moves out of a certain threshold, to try to accomplish this I have the following if statement:

 if(((Mathf.Abs(target.transform.position.x))-(Mathf.Abs(transform.position.x)))>thres){
x = moveTo(transform.position.x, target.position.x, trackSpeed);
}


in unity/c#. In pseudocode it means

if((absolute value of player x) - (absolute value of camera x) is greater than the threshold){
move
{


however this does not seem to work correctly. it appears to work for the first couple of times the threshold is reached, however the distance between the camera and the player has to increase every time for the camera to move. I do not believe the movement of the camera is the problem, however the code for it is as follows:

private float moveTo(float n, float target, float accel) {
if (n == target) {
return n;
}
else {
float dir = Mathf.Sign(target - n);
n += accel * Time.deltaTime * dir;
return (dir == Mathf.Sign(target-n))? n: target;
}
}


}

• The "if" comparison in your moveTo function should be changed to Math.Abs(float1 - float2) <= difference. Where the difference is the amount it can vary. Since comparing floats with "==" is not very likely to trigger. – Esa Dec 10 '13 at 6:42
• Watch this youtube.com/watch?v=Z38cRQ3yQqA – Savlon Mar 4 '15 at 12:57
• Try calling moveTo() from LateUpdate() instead of Update. It's a good practice to have camera movements managed through LateUpdate() rather than Update. – SanSolo Oct 31 '15 at 17:16

i dont like

(Mathf.Abs(target.transform.position.x))-(Mathf.Abs(transform.position.x))

(Mathf.Abs(target.transform.position.x-transform.position.x))


try it

I don't have a direct answer for you, but good debugging will help. I suggest that you use Debug.Log in the Unity Editor. For example, log whether it has chosen to move, and if so where it has moved to. Often that gives way too much output, which is where I'd suggested throttling it, for example only log if Time.frameCount % 10 == 0. This will only log every tenth frame. Then reproduce your bug, pause the game, and look at the log.

• Search google for creating a smooth follow camera. I have a script I could show you that stops camera movement when the player reaches the edge of the level and the player moves without the camera following but I don't think that is what you're after? – Savlon Feb 18 '14 at 9:08

Here's the problem:

In pseudo-code, what you're doing is this:

if ( abs(playerX) - abs(cameraX) > 5 ) // arbitrary threshhold value of 5
moveCamera();


Let's take the case where the player is at 15, and the camera is at 100. It's obvious to you and me that in this situation, the camera is too far away and ought to move, but let's run the math:

abs(15) == 15
abs(100) == 100
15 - 100 == -85


-85 isn't greater than 5, so the camera won't move!

Your problem is that you don't want to be taking the absolute value of each position separately and then subtracting them; you want to be taking the absolute value of the difference between the two positions! That way, you'd get abs(15-100) == 85 and 85 > 5, so the camera would correctly move.