- Home /
Help with camera coding - Modern Zelda-style camera
Ladies, gentlemen.
Currently, I'm attempting to set up a camera system akin to the modern Zelda titles, wherein the camera stays a certain distance from the player, and will rotate to look at them if they move to either side, but not if they move either away from or towards it.
The basic Smooth Follow camera does the side rotations moderately well, but the issue I keep having is that when the player moves toward the camera, it freaks out and rotates around the player incredibly fast rather than just staying put and moving with them.
Any insight?
~~EDIT~~
So I tried adding a bit that would only have the Smooth Follow's rotation bits take effect if the player's rotation isn't between 5 more than and five less than the camera's rotation (Facing the camera), or between 175 and 185 more than the camera's rotation (Facing away):
if(ActivePlayer.rotation.y >= transform.rotation.y + 5 && ActivePlayer.rotation.y <= transform.rotation.y - 5
|| ActivePlayer.rotation.y >= transform.rotation.y + 185 && ActivePlayer.rotation.y <= transform.rotation.y + 175){
//Do rotate
}
else
//Don't rotate
Sadly, this didn't work for some reason. Was there a misstep in my logic or something?
Answer by testure · Sep 18, 2011 at 09:38 PM
I have a similar camera setup in my game- and I've solved this by writing my own camera handler from scratch. Based on my experience as a 3D artist, I've actually built the camera in a way that a 3D modeling application would build it- Using a root, a camera, and a target.
The heirarchy looks something like this:
Camera root
--> Camera
--> Target
The root is used for moving and rotating the camera, and the camera itself is only set at the optimal height and distance away from the root. The camera always looks at the target- this makes it easy to smoothly look at different things, rather than transform.LookAt snapping to a new transform.
So in my game, I simply match the root's position to the player's position, and as the player moves around the camera will always be at the designated distance and height.
There's other logic that handles smooth transitions, orbiting around the player, and occlusion culling of course, but that's beyond the scope of this question.. and a very long topic in itself.
Hopefully this helps a bit. The key thing with my setup is that I don't really move the camera at all (unless the player has changed the height or distance of the camera)- I move the root.
So, this Root... Is it parented to the target, or is there a script attached that makes it copy the target's position?
No, the root is the root of the heirarchy. the children of root automatically follow root because they are parented to it.
Ah, okay. Either way, I was able to make a suitable setup derived from your method. Thank'eh!
Say, have you ever experienced issues with the camera randomly warping about, regardless of where the parent node is?