纳金网

标题: 第三视角角色移动控制基础教程(二) [打印本页]

作者: 会飞的鱼    时间: 2011-12-22 15:24
标题: 第三视角角色移动控制基础教程(二)
Finishing touches



If you try to play the game now, you will see the character moving around. Fantastic!!
He seems a little stubborn and doesn’t want to turn at all though. We will fix that next.
transform.rotation = Quaternion.LookRotation(moveDirection);

this takes the transform (the main orientation of game object), and rotates it by moveDirection. We already set up moveDirection earlier for the angle we want it to be at.
Success…but wait. If you take your hand away from the controls for a second, he will auto-magically rotate back facing right. Dang!
So close. One more thing to fix that. Wrap this if statement around the last line you put in. It should look like this.
if (targetDirection != Vector3.zero) { transform.rotation = Quaternion.LookRotation(moveDirection); }

What this does is that the game object will only try to rotate if the targetDirection is NOT zero. When you take you hand away from the keyboard for a split second, the input values will show (0,0,0), so it thinks that is the target direction.
It isn’t,but it doesn’t know that. The only time that value will show is when you take your hands off, so the if statement simply ignores input when you take your hands off everything.
One last thing. Add this after the entire Update function at the bottom:
@script RequireComponent(CharacterController)

This will make sure a Character Controller component is attached to the game object that the script is attached to. If you forget to add one and you press play, this line will automatically add a component to the game object and save you from a nasty error.
Hit play and watch your character move and rotate around.
Final Thoughts



If for some reason you scrolled down to get the source without reading anything, here is the project set up files and source.
This is a good first step in understanding how basic character movement is put together. A fully done character is complicated, but understanding these concepts are fundamental to building more advanced setups. This set up might seem weird with how the character moves, but when a camera is added along with a few other elements later, it will all make sense. I will try to continue where this example leaves off and explain more with collisions and actions like jumping in the next post.
由 uke  发表
作者: ly    时间: 2011-12-23 20:34
路过顶下吧、、、、
作者: C.R.CAN    时间: 2012-1-19 23:04
年年岁岁,岁岁年年,春去春来,转眼间又过了一年,小年到,祝福到,愿你在新的一年事业节节高,生活红火火,拜个早年,提前祝你新年好!

作者: 奇    时间: 2012-1-20 23:18
除夕夜,春节倒计时,快乐元素在积聚,欢乐军团急行军,迫不急待的我,与新年阳光一道启程,把一年祝福提前给你,祝你红红火火过虎年。

作者: 晃晃    时间: 2012-2-1 23:22
发了那么多,我都不知道该用哪个给你回帖了,呵呵

作者: 菜刀吻电线    时间: 2012-3-21 23:24
不错哦,顶一下......

作者: C.R.CAN    时间: 2012-6-26 23:25
水……生命之源……灌……

作者: 晃晃    时间: 2012-7-5 23:25
很有心,部分已收录自用,谢谢

作者: BU Secret    时间: 2012-7-6 15:11
好好好好好好好好好好好好好好好
作者: BU Secret    时间: 2012-7-6 15:11
好好好好好好好好好好好好好好好
作者: Eleven    时间: 2012-7-6 15:14
单反系好盆友的帖子都要顶!
作者: 艺术家    时间: 2012-7-6 16:07

    好好好好好好好好 给力给力

作者: 艺术家    时间: 2012-7-6 16:08

    好好好好好好好好 给力给力

作者: 晃晃    时间: 2012-8-5 00:08
顶!学习了!阅!

作者: dzspb    时间: 2013-7-31 10:25
翻译过来更好




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5