Boolean variable doesn't change back

I have never had such problem before. It’s absolutely unexplainable to me.
I got those lines:

if(Vector3.Distance(transform.position, linecastInfo.point) <= 3) isCameraClose = true;
else isCameraClose = false;

So if the distance between those two points is less or equal to 3, isCameraClose would be true, otherwise, it’s going to be false.
Yeah, great…the problem is that after if becomes true it doesn’t change back to false, no matter what the distance between those points is. I am not changing the variable to true anywhere else in the script infact I’m not using it anywhere else except in these lines yet. What could it be that is making the variable remain true?

Where do you call that code? Inside update or inside a function that only gets called once.
How do you output the value of the variable so you know it is true/false?

Probably your linecast isn’t returning true OR it’s returning true but it’s not hitting the collider you assume. Maybe once you’re camera is that close the linecast starts originating from inside the collider you are assuming you should hit, and therefore it’s never hitting it and hitting something else instead. Raycasts only hit the exterior of colliders, I don’t know if this is true of linecasts.

It could be that it’s working, but you have “collapse” turned on in the console.

–Eric

An easy way to solve the problem would just be

Debug.Log("Distance to point: " + Vector3.Distance(transform.position, linecastInfo.point));

Sorry for the tardy reply. Now:

  • I’m calling this inside Update
  • I’m outputing the result with print/Debug.Log …both - the same
  • No, I don’t have collapse turned on
  • If my Linecast doesn’t return true, isCameraClose would become false in an else statement

Here’s a bigger part of the code:

if(Physics.Linecast(transform.position, linecastReceiver.transform.position, linecastInfo, 9)){
cameraTarget.transform.position = linecastInfo.point;
cameraTarget.transform.localPosition.z += 0.7;
if((transform.position - linecastInfo.point).magnitude <= 3) isCameraClose = true;
else isCameraClose = false;
} else{
isCameraClose = false;
cameraTarget.transform.position = Vector3.Lerp(cameraTarget.transform.position, camGoTo.transform.position +  (camGoTo.transform.forward/2), positionDamp * Time.deltaTime );
}
  • And yes, I already tried debugginh Vector3.Distance, everything’s OK there.

So that’s why this seems so strange to me and I bet id does to you too.

Well then the problem is somewhere else.

Try changing isCameraClose to a Property instead of a field

public isCameraClose {
get {
return _isCameraClose;
}
set {
Debug.Log(value);
_isCameraClose = value;
}
}

private _isCameraClose = false;

The log should give you the stack-trace before Debug.Log(value) is called… find ones that say “True” and check the trace.