Camera movement and threshold not working

Posted by irish guy mcconagheh on Game Development See other posts from Game Development or by irish guy mcconagheh
Published on 2013-12-08T23:33:42Z Indexed on 2014/06/07 9:39 UTC
Read the original article Hit count: 262

Filed under:
|
|
|

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

}

© Game Development or respective owner

Related posts about c#

Related posts about unity