Unity3D: How to make the camera focus a moving game object with ITween?
- by nathan
I'm trying to write a solar system with Unity3D. Planets are sphere game objects rotating around another sphere game object representing the star.
What i want to achieve is let the user click on a planet and then zoom the camera on this planet and then make the camera follow and keep it centered on the screen while it keep moving around the star.
I decided to use iTween library and so far i was able to create the zoom effect using iTween.MoveUpdate. My problem is that the focused planet does not say properly centered as it moves.
Here is the relevant part of my script:
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, concernedLayers))
{
selectedPlanet = hit.collider.gameObject;
}
}
}
void LateUpdate() {
if (selectedPlanet != null)
{
Vector3 pos = selectedPlanet.transform.position;
pos.z = selectedPlanet.transform.position.z - selectedPlanet.transform.localScale.z;
pos.y = selectedPlanet.transform.position.y;
iTween.MoveUpdate(Camera.main.gameObject, pos, 2);
}
}
What do i need to add to this script to make the selected planet stay centered on the screen?
I hosted my current project as a webplayer application so you see what's going wrong. You can access it here.