How to get this script to run once when a boolean is true

I’ve written a script that is supposed to move a camera from a FPS view to a 3rd person view, if a boolean is true/false.

The boolean fpCam is toggled on and off with a GetbuttonDown(“View”) which is V.

So this script needs to constantly check if the fpCam is true or false, so I’ve placed it in the Update function.

However my script runs repeatedly as long as fpCam is true or false, rather than just once.

What I want is, when someone presses V, the camera smoothly moves from position A to B, so I only want the script to run once when the boolean changes, rather than for as long as it is true/false.

When I hit V the views do change but very suddenly, with no smoothing at all, and the camera seems to vibrate/jitter constantly, which I suspect is the code being run repeatedly. When I turn the velocity up (velT/velF) the jittering gets much worse, and when I lower it it’s almost unnoticeable, and smooth seems to do nothing.

Can any help please?

function Update()
{
//First/Third Person View Controls:
//If Button is pressed switch between 1st/3rd person view
	if (Input.GetButtonDown ("View"))
	{
	//Toggle First/Third Person View Positions
		fpCam = !fpCam;
	}
	
	//If First Person View is equal to true, then move camera to FPVPoint and resolve crosshairs
	var smooth : float = 5.0;
	var velT : float = 1.0;
	var velF : float = 1.0;
	if (fpCam == true)
	{		
		var moveCamFromX = Mathf.SmoothDamp(tpvPoint.transform.position.x, fpvPoint.transform.position.x, velT, smooth);	
		fpCamera.transform.position.x = moveCamFromX;
		
		var moveCamFromY = Mathf.SmoothDamp(tpvPoint.transform.position.y, fpvPoint.transform.position.y, velT, smooth);	
		fpCamera.transform.position.y = moveCamFromY;
		
		var moveCamFromZ = Mathf.SmoothDamp(tpvPoint.transform.position.z, fpvPoint.transform.position.z, velT, smooth);	
		fpCamera.transform.position.z = moveCamFromZ;
		
		fpcrosshair.active = true;
		tpcrosshair.active = false;
	}
	////If First Person View is equal to false, then move camera to TPVPoint and resolve crosshairs
	else
	{
		var moveCamToX = Mathf.SmoothDamp(fpvPoint.transform.position.x, tpvPoint.transform.position.x, velF, smooth);	
		fpCamera.transform.position.x = moveCamToX;
		
		var moveCamToY = Mathf.SmoothDamp(fpvPoint.transform.position.y, tpvPoint.transform.position.y, velF, smooth);	
		fpCamera.transform.position.y = moveCamToY;
		
		var moveCamToZ = Mathf.SmoothDamp(fpvPoint.transform.position.z, tpvPoint.transform.position.z, velF, smooth);	
		fpCamera.transform.position.z = moveCamToZ;
		
		fpcrosshair.active = false;
		tpcrosshair.active = true;
	}
}

Anyone?

Let me try…

function Update() {
     if (Input.GetButtonDown("View")) {
          if (fpCam) {
               zoomOut();
          } else {
               zoomIn();
          }
          fpCam = !fpCam;
     }
}

function zoomOut() {
     //From first person to third person
}

function zoomIn() {
     //From third person to first person
}

Hmm, I tried your method, seems like a good way to do it, although, the view does switch fine when I hit V, but it’s switching suddenly back and forth.

Maybe I’m mistaken about the functionality of Mathf.SmoothDamp, I thought that by using this I could have the camera move smoothly back and forth between the two views. Like actually translate smoothly from point A to point B.

Should I be using .translate itself?

Um… Before trying to fix something, we need to be sure what is going wrong :stuck_out_tongue:

To know if it’s my method or Mathf.SmoothDamp that is causing the troubles, just set fixed position in zoomIn and zoomOut without smoothing it.
If it still swith back and forth, this is my method. Else, this is Mathf.SmoothDamp.

Here’s my current code, I think because I’m changing the transform instantly I’m getting no smoothing effect, so I need to either multiply it by a time somehow or maybe use translate. I’ve tried many many methods now and it’s only making less and less sense to me.

Anyway I commented out Mathf.SmoothDamp, and added some code telling the camera exactly where to go, bypassing Mathf.SmoothDamp, and I’m still getting an instant transition from view A to view B.

What I’m looking for is a smooth and slow movement from point A to point B.

Any help at all is appreciated, thank you :]

//First/Third Person View Controls:
//If Button is pressed toggle view boolean
	if (Input.GetButtonDown ("View"))
	{
	//Toggle First/Third Person View Positions
		fpCam = !fpCam;
	}  
	var smooth : float = 5.0;
	var vel : float = 1.0;
	
	if (Input.GetButtonDown ("View"))
	{  
	//If First Person View = True, then move camera to TPVPoint and resolve crosshairs
		if (fpCam == true)
		{
			//var moveCamFromX = Mathf.SmoothDamp(fpvPoint.transform.position.x, tpvPoint.transform.position.x, vel, smooth);
			//var moveCamFromY = Mathf.SmoothDamp(fpvPoint.transform.position.y, tpvPoint.transform.position.y, vel, smooth);
			//var moveCamFromZ = Mathf.SmoothDamp(fpvPoint.transform.position.z, tpvPoint.transform.position.z, vel, smooth);
			
			//fpCamera.transform.position.x = moveCamFromX;
			//fpCamera.transform.position.y = moveCamFromY;
			//fpCamera.transform.position.z = moveCamFromZ;
			
			fpCamera.transform.position.x = tpvPoint.transform.position.x;
			fpCamera.transform.position.y = tpvPoint.transform.position.y;
			fpCamera.transform.position.z = tpvPoint.transform.position.z;
			
			fpcrosshair.active = false;
			tpcrosshair.active = true;		
		}
	//If First Person View = False, then move camera to FPVPoint and resolve crosshairs
		else
		{
			//var moveCamToX = Mathf.SmoothDamp(tpvPoint.transform.position.x, fpvPoint.transform.position.x, vel, smooth);   
			//var moveCamToY = Mathf.SmoothDamp(tpvPoint.transform.position.y, fpvPoint.transform.position.y, vel, smooth);   
			//var moveCamToZ = Mathf.SmoothDamp(tpvPoint.transform.position.z, fpvPoint.transform.position.z, vel, smooth);   
			
			//fpCamera.transform.position.x = moveCamToX;
			//fpCamera.transform.position.y = moveCamToY;
			//fpCamera.transform.position.z = moveCamToZ;
			
			fpCamera.transform.position.x = fpvPoint.transform.position.x;
			fpCamera.transform.position.y = fpvPoint.transform.position.y;
			fpCamera.transform.position.z = fpvPoint.transform.position.z;
			
			tpcrosshair.active = false;
			fpcrosshair.active = true;
		}
	}