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:

            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){

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; 


  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Esa Dec 10 '13 at 6:42
  • \$\begingroup\$ Watch this youtube.com/watch?v=Z38cRQ3yQqA \$\endgroup\$ – Savlon Mar 4 '15 at 12:57
  • \$\begingroup\$ Try calling moveTo() from LateUpdate() instead of Update. It's a good practice to have camera movements managed through LateUpdate() rather than Update. \$\endgroup\$ – SanSolo Oct 31 '15 at 17:16

i dont like


Instead I'd like


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.

  • \$\begingroup\$ 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? \$\endgroup\$ – 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

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.