unity3d第三人称角色控制教程-5
If you've been following our 3rd person character controller for Unity tutorial so far you will have a playable character in your game world, however there are two flaws with the system we have created.
1. The camera does not follow the character.
2. The character can walk up very steep/sheer inclines on the terrain.
Of course we shall now address both of these issues.
Camera Following
When the character is walking we want the camera to follow the character from behind. There are three common ways of doing this:
1. Dragging the Main Camera in the Hierarchy onto the character prefab in the Hierarchy, which binds the camera to the controller.
2. Creating a smooth follower camera, which is bound to the character
3. Creating an orbital camera.
The first method is the easiest, just drag the camera in the hierarchy onto the character in the hierarchy to set the camera as a child of the character. This is easy and works if you want to test it out. However the movement is a bit harsh, its not the nicest follower.
With the second method, we apply a smooth follow component to the camera and attach it to the character, which results in a much nicer follower.
The third method is a bit more complex and will be the subject of another tutorial which is coming soon. So we will implement the second method in this tutorial. So follow these simple steps:
1. Select the camera in the hierarchy
2. Select Component -> Camera Control -> Smooth Follow
This has applied a smooth follow component to the camera, we now need to attach it to the character.
1. Select the Main Camera in the Hierarchy.
2. In the Inspector look for the Smooth Follow script section pictured below.
3. Set the target to the character transform.

If you test the game now the camera will follow the character but will be out of position; so we need to set the cameras position.
So set the Height and Distance settings of the smooth follow camera so that it is in the desired position. When you've got it in the right place test the game, you'll find that the camera now follows the character and smoothly moves with the character and not harshly which makes for a much more pleasant camera motion.
Slope Limits
If you've sculpted some terrain (such as hills or cliffs) then you will notice that the character can walk up even a sheer vertical incline, which is impossible; so we want to prevent this. It is actually very easy to set a slope limiter to prevent the character walking up an incline of x degrees, because the character controller component we attached to the character has a slope limiter built in, its just got a very steep allowance at the moment.
1. Select the character in the Hierarchy
2. Under the character controller section in the Inspector look for "Slope Limit"

It has a default of 90 degrees, which is pretty steep, so lets change it to 40-50 degrees. Now if you havent done so already, sculpt some geometry onto your terrain and test it out, adjusting the slope limit until you feel its at a comfortable limit for your game.
Now in part 6 we shall be concluding our tutorial with a quick discussion on controllers and some final thoughts.
So you've completed the bulk of the 3rd person character controller tutorial, while it works pretty well its very basic and will want extending... which was kind of the point of this tutorial; to provide the bare bones; basic controller for you to extend for your games as opposed to bogging you down in game specific mechanics.
If you understood the code in the script you should be able to add more actions now, like if you have a punch animation how to read in a button press to play that animation and how to add some logic into the script; things like that.
This final section mentions some final thoughts.
Final Thoughts
Follower Cameras
While the follower camera works, you do need to consider camera collision; but to do that you need a grasp on other mechanics which are outside of the scope for this tutorial, we'll be adding an advanced camera control tutorial very soon so watch this space.
Reading Animation Time
Its useful to know how to read in the time of an animation for when you perform actions; its quite simple, this code will return the duration of an animation:
animationTime = animation["name_of_animation"].clip.length;
Applying Textures to your Character
We didn't cover how to apply textures to your character; this is quite simple and is the same for any model import. Import the textures individually, open the materials and apply the texture for each material in the Inspector.
此系列教程全部结束!
相关项目文件点此链接下载!


评论
4 一月 2012
19 周 3 天
教程很简洁,易懂