Hi there
I’m currently creating a game and I’m having some trouble with camera movements. The camera starts with being able to see the whole stage during the menu navigation and once the user hits play game, the camera zooms closer to the object on the stage so you can only see the table (I’m making a cup and ball game) as opposed to the whole room.
SetCameraPosition(Vector3(GameObject.Find("CamPosGame").transform.position.x, GameObject.Find("CamPosGame").transform.position.y, GameObject.Find("CamPosGame").transform.position.z), Vector3(0, GameObject.Find("CamTargetGame").transform.position.y, 0),0);
function SetCameraPosition( positionTarget : Vector3, facingTarget : Vector3, buffer : float )
{
//Move the camera towards the Altar, with easing
//
//var distanceTravelled : float = Vector3.Distance( camStartPos, Camera.main.transform.position ); //How far is left?
//var totalDistance : float = Vector3.Distance( camStartPos, positionTarget ); //Total distance?
//var moveVar : float = distanceTravelled / totalDistance;
var distanceTravelled : Vector3;
var totalDistance : Vector3;
var moveVar : Vector3;
distanceTravelled.y = Vector3.Distance( Vector3( 0, camStartPos.y, 0 ), Vector3( 0, Camera.main.transform.position.y, 0 ) );
totalDistance.y = Vector3.Distance( Vector3( 0, camStartPos.y, 0 ), Vector3( 0, positionTarget.y + 0.1, 0 ) );
moveVar.y = distanceTravelled.y / totalDistance.y;
distanceTravelled.z = Vector3.Distance( Vector3( 0, 0, camStartPos.z), Vector3( 0, 0, Camera.main.transform.position.z) );
totalDistance.z = Vector3.Distance( Vector3( 0, 0, camStartPos.z), Vector3( 0, 0, positionTarget.z + 0.1) );
moveVar.z = distanceTravelled.z / totalDistance.z;
//Debug.Log( "Distance Travelled : " + distanceTravelled.z );
//Debug.Log( "Total Distance : " + totalDistance.z );
//Debug.Log( "Cam Start Pos : " + camStartPos.z );
//Debug.Log( "Position Target : " + positionTarget.z );
//Debug.Log( "Move Var : " + moveVar.z );
//Setup Z movement
if( moveVar.z == 0 )
moveVar.z = 0.1;
moveVar.z = moveVar.z * 2;
if( moveVar.z > 1 )
{
moveVar.z = moveVar.z % 1;
moveVar.z = 1 - moveVar.z;
}
if( moveVar.z == 0 )
{
moveVar.z = 0.1;
}
moveVar.z = Mathf.Clamp( moveVar.z, 0, 0.5 );
//Debug.Log( moveVar.z );
if( camStartPos.z > positionTarget.z ) //Check for neg or pos direction
{
moveVar.z *= -1;
}
Camera.main.transform.position.z += moveVar.z * Time.deltaTime * camSpeed;
if( Camera.main.transform.position.y > positionTarget.y)
{
//Setup Y movement
if( moveVar.y == 0 )
moveVar.y = 0.1;
moveVar.y = (moveVar.y) * 2;
if( moveVar.y >= 1 )
{
moveVar.y = moveVar.y % 1;
moveVar.y = 1 - moveVar.y;
}
// if( moveVar.y == 0 )
//{
// moveVar.y = 0.1;
// }
moveVar.y = Mathf.Clamp( moveVar.y, 0, 0.5 );
Camera.main.transform.position.y -= moveVar.y * Time.deltaTime * camSpeed;
}
//Debug.Log( moveVar );
//Move the camera, smoothing on each end -- NOTE: This equation took a lot of revision, so don't mess with it unless you have a backup, and really know what you're doing.
//Camera.main.transform.position.z += Mathf.Abs( (moveVar.z + 0.1) * Mathfx.Hermite( camStartPos.z, positionTarget.z, moveVar.z ) ) * Time.deltaTime * camSpeed;
//Debug.Log(Mathf.Abs( (moveVar.z + 0.1) * Mathfx.Hermite( camStartPos.z, positionTarget.z, moveVar.z ) ) * Time.deltaTime * camSpeed);
//if( Camera.main.transform.position.y >= positionTarget.y + buffer + 1 ) //Constrain so we don't fall too low
//{
// Camera.main.transform.position.y -= Mathf.Abs( (moveVar.y + 0.1) * Mathfx.Hermite( camStartPos.y, positionTarget.y, moveVar.y ) ) * Time.deltaTime * camSpeed;
//}
//if( Camera.main.transform.position.y >= positionTarget.y ) //Constrain so we don't fall too low
//{
// Camera.main.transform.position.y -= 0.035;
//}
//Rotation
var targetRotation = Quaternion.LookRotation( facingTarget + Vector3( 0, 0, 0) - Camera.main.transform.position, Vector3.up ); //Find the rotational vector that will bring us to face the Target
Camera.main.transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime); //Rotate towards said target
//Debug.Log( distanceTravelled.z + " :: " + totalDistance.z + " :: " + moveVar.z);
//Check to see if we're in position yet
if( Camera.main.transform.position.z >= (positionTarget.z) )
{
bSetGameCam = false; //once camera reaches positionTarget, stop game cam and reveal shells
bButtonsActive = true; //activate buttons
}
if( Camera.main.transform.position.z <= (positionTarget.z) )
{
if( bSetupEndChoice )
{
bSetupEndChoice = false;
}
}
//if return button is selected, return to main menu position
if (Camera.main.transform.position.z <= -13 && bReturn)
{
bReturn = false;
bButtonsActive = false;
}
}
That’s the code for the camera to zoom into the table. My question is I want the camera to zoom back out to the menu navigation position when the user hits the “return to main menu button”
How would I go about doing that?