- Home /
Make Camera Follow Another GameObject
I've done quite a bit of research on this and haven't been able to find anything about this.
I have two GameObjects which in instated when the user logs in: the first is the player and the second is a horse, depending on what horse the user has. When the user mounts the horse, it switches cameras for usability's sake. When the user mounts the horse, the camera follows the horse specifically, but it is not a child of the horse because it's not smooth. Here is the hierarchy of one of the prefabs (they're all alike):
![alt text][1]
When the user first mounts, the view is something like this:
![alt text][2]
After the user starts turning, I get this:
![alt text][3]
I want the camera to be centered like it was when the user first mounted the horse. This is how I currently make the camera follow the horse:
//THIS SCRIPT IS ATTACHED TO THE HORSE DIRECTLY
transform.position += transform.forward * speed * Gait * Time.deltaTime;
horseCamera.transform.position += transform.forward * speed * Gait * Time.deltaTime;
horseCamera.transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
If any of this doesn't make sense I'll try to explain it better.
Thanks in advance! [1]: http://photos.app.goo.gl/MWYXW7zriY9JgjkK9 [2]: https://photos.app.goo.gl/edvm94hEMfqyueE39 [3]: https://photos.app.goo.gl/76S5JNR9UMbr7Yoe7
Answer by Ermiq · Nov 25, 2019 at 11:27 AM
You say the horse camera is not a child of the horse but at the first picture the Camera
object is actually a child of the Horse_White
.
What is the object the script you've posted is attached to? If you have the script attached to the Horse_White
then you move the whole Horse_White
object with all its children by the script, therefore your camera will move too and you don't need to move the camera in the script then. By changing camera position in this case you make it go away off the horse and that's why you get this offset.
Another option: you have the script attached to the Horse
object which is a child of the Horse_White
. You shouldn't do that. You should only move the root object in the hierarchy (the parent, Horse_White
in your case). By moving just the Horse
you leave the parent at the place where it was at the start, and it's another reason why you could get this offset, because you move Horse
(along with Armature
) but your root object doesn't move and your camera doesn't move with the root as well. You forcibly move the camera in the script by changing its position
property so it gets offset from the root and from the Horse
as well (if their position changes are not calculated exactly the same way). And even if you have the same code for Horse
moving and camera moving it still doesn't mean that they will move similar ways.
See, there are different position types. position
and localPosition
. position
is a position of an object relative to the world coordinates (i.e. the whole scene), while localPosition
is a position relative to the parent game object.
When you move an object that has no parents, you actually move this object and all its children. Its children localPosition
is not changing in this case, if you place the camera behind the horse and just move the Horse_White
the camera will follow and will stay behind the horse.
When you move a child by changing its position
you actually move it away from the parent, in world coordinates system, and you change its localPosition
relative to the parent as well.