2
\$\begingroup\$

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; 
    }
}

}

\$\endgroup\$
3
  • 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
    Commented Dec 10, 2013 at 6:42
  • \$\begingroup\$ Watch this youtube.com/watch?v=Z38cRQ3yQqA \$\endgroup\$
    – Savlon
    Commented Mar 4, 2015 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
    Commented Oct 31, 2015 at 17:16

3 Answers 3

1
\$\begingroup\$

i dont like

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

Instead I'd like

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

try it

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\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
    Commented Feb 18, 2014 at 9:08
0
\$\begingroup\$

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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .