How to save position of camera

I have a third person camera that can orbit around the player and(when needed) reposition and lock itself behind the player for targeting purposes. I want to make it so when switching from targeting to orbit the camera will keep the position and not snap back to the orbit position it had.

void UpdatePosition()
	{
		if(!Targeting2.Instance.isTargeting)
		{
			var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
			var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
			var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
			position = new Vector3(posX, posY, posZ);
		
		transform.position = position;
		transform.LookAt(TargetLookAt);
	}
	else if(Targeting2.Instance.isTargeting)
	{
		var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_TSmooth);
		var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_TSmooth);
		var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_TSmooth);
		position = new Vector3(posX, posY, posZ);
		
		transform.position = position;
		transform.LookAt(TargetLookAt);
	}
}

Can't you just save the position to a variable, and then when you need to snap back just set the position to the variable.

1 Answer

1

Somewhere (not in the snippet you have) you are computing desiredPosition. For orbit, it’s probably based on an orbit-angle variable.

When you switch into Orbit mode, also reset that angle to 0 (or whatever number means forwards.)

if(Targeting2.Instance.isTargeting) desiredPosition = Player.transform.TransformPoint(0, 15, -20); else desiredPosition = CalculatePosition(mouseY, mouseX, Distance); This is what I have for calculating desiredPosition. So what your saying I should do is make desiredPosition reset to the istargeting position whenever I switch over. I have tried setting the desiredPosition just before I calculate it but is doesn't seem to work It still goes back to the old position.

"setting the desiredPosition just before I calculate it...goes back to the old position": Right. It's recomputed each frame from mouseX and mouseY, so those are what they reset.