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