Camera movement and threshold not working
- by irish guy mcconagheh
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;
}
}
}