Unity3D: How to make the camera focus a moving game object with ITween?
Posted
by
nathan
on Game Development
See other posts from Game Development
or by nathan
Published on 2013-10-18T17:09:04Z
Indexed on
2013/10/18
22:19 UTC
Read the original article
Hit count: 338
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.
© Game Development or respective owner