Let's dive in deep!
In the beginning, there was an idea of mine, so I fired up Unity. First problem I wanted to solve is how to position the camera above and behind the player. My first idea was to just create a script that receives an object, a distance and a height, and tries to calculate the correct position for the camera. But I am lazy and I hate to think.
So I wanted to offload this work to Unity. I figured out I could just use Unity's object hierarchy to have the camera always properly placed. Just make the camera a child object of the player, and it will always be behind and above the character, no matter where it goes or turns (as long as it turns only around the up axis, that is).
But I want the camera to follow the player smoothly. Because of this, I won't put the actual camera in the hierarchy. Instead, I'll just use an empty proxy, and I will tell the camera to smoothly follow that proxy object and face the same direction as that proxy.
So now we have a Player, with two children: PlayerModel and CameraProxy. (Actually I could just put the playermodel into the root object, but for some reason, my hunch says it is a bad idea. We'll see.)
I attach an ObjectFollower script to the camera, and give the proxy as its argument. (The value I use for moving speed is 100, and the rotation speed is 10.)
(And I just noticed I forgot to actually upload the Assets folder to my github repo, because I accidentally put it in my .gitignore. Silly me.)
Here's a link to the ObjectFollower script.
All it does is moves the follower object closer and closer to the followed object, and also rotates it, in a smooth manner.
Next thing I wanted to do is to provide physical constraints. I've set up a test room, with walls. I wanted the player (denoted by a sphere for now) to not go through them. So I've added a Rigidbody component to the Player object (the root object), froze the rotation in all directions (it means that the physics engine won't rotate it, while scripts still can), and then added a Sphere Collider for the PlayerModel object (one of the child objects), so that it has an actual physical body.
I also attached a Controllable component (here is the code) for the Player object, in order to let the player control it. (I love how the line endings get messed up every time in my codes. /s)
And I saw this was good. And there was evening and there was morning, the first day.
No comments:
Post a Comment