- Home /
Move camera on the z axis forward and backward with coroutine
Hi, I'm making a simulation of a desktop in a 3d space, and I need to move the main camera along the z axis forward when I am over a 3d icon and backward when I left the icon with the mouse. Here is my code but the coroutines can't work together in the way I need:
IEnumerator Zoom (float zPos, float delay)
{
yield return new WaitForSeconds (delay);
float elapsedTime = 0.0f;
while (elapsedTime < 2.0f) {
elapsedTime += Time.deltaTime;
Camera.main.GetComponent<FreeMovement> ().zPos = Mathf.Lerp (Camera.main.transform.position.z, zPos, elapsedTime / 2.0f);
yield return null;
}
}
And I call the coroutine here:
void OnMouseEnter ()
{
StartCoroutine (Zoom (50.0f, 0.0f));
}
void OnMouseExit ()
{
StartCoroutine (Zoom (0.0f, 2.0f));
}
The script is attached to every icon in the space, the zPos is controlled over the FreeMovement script attached to the main camera.
The result is that zooming in is ok but zooming out takes place for the next zoom in. I've tried to add a delay but what I really need is a coroutine that results in zooming in while mouse enter and zooming out from the last position on mouse exit towards 0 without jumps. Is it possible?
So when you put your mouse over icon it zooms but does not zoom out until your mouse is over a different icon, even if you wait 2 seconds before hovering over next icon? If that is the problem then it is most likely something to do with the script that is responsible for checking when you are and when you are not hovering over an icon.
No, when I put the mouse on an icon, it zooms in and if I exit the icon it zooms out only if I wait 2 seconds... but I need that if I exit from the icon, the camera moves backward from the actual position.
Your answer
Follow this Question
Related Questions
Issue With Simultaneous ReadPixels Calls 0 Answers
Orbit Camera Zoom limit 1 Answer
How to get a camera state to wait with coroutine? 0 Answers