12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 6805|回复: 13
打印 上一主题 下一主题

第三视角角色移动控制基础教程(一)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-12-22 15:23:25 |只看该作者 |倒序浏览


初始项目文件在附件中

My original version of doing movement was a pretty sloppy hacked version. I provided the source, but it was almost impossible to really figure out what was going on. I didn’t really know what was happening, but felt good because it worked. I went back and really cut down all the code to the minimum and explained it with a lot more comments. This post explains some of the basic elements to get that 3rd person character moving and rotating.
On the 3rd person character controller script with the 3D platform tutorial that Unity provides, there is over 450+ lines of code describing how the character moves around. After studying it for a while, the most important parts of the script can be hard to spot with all of the different variables and other functionality. For this post, I just wanted to narrow it down to basic movement and rotation.
My example  still has about 30 lines of code for the basics (over 40 with commenting).
Controls: W,A,S,D move OR arrow keys

效果请下载附件查看


Setup:
If you want to start from scratch, you can start a new project and add an object you want to control and move. You can also start with a setup project that I made. There is no gravity, physics, or collisions implemented with this explanation/tutorial, so you don’t have to create a ground plane for your character to fall on. Just add a gameObject to the stage and add a script to the object. Add a character controller component to your character game object.  If you are using my set up file, just open the charactermovement.js script and you’re ready.
Nothing happens if you play the game now, since there is only one script and it is empty. Everything from this point on will be dealing with the script attached to the game object you want to move.
private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController);   private var moveDirection = Vector3.zero; private var forward = Vector3.zero; private var right = Vector3.zero;

These first lines are declared at the top of the script. They aren’t in any functions or classes. This is so that any function inside of the script can access the variables. Making them private means they can’t be accessed by the inspector.
The first line creates a variables of type CharacterController. This is a class that is used to manage movement of your controllable character. The second line sets the controller variable to the character controller component. “gameObject” is used to access the current gameobject that the script is attached to. The “GetComponent” method searches the game object for components that are attached to it. We can reference “controller” any where in the script if we want to use any character controller functions or methods.
The last three lines are setting up the movement direction and forward directions to “Vector3.zero”. This is shorthand for making a new vector3 object with the value (0,0,0). These will store the absolute move direction along with the relative forward direction.
Update function

Since movement is constantly changing, all movement and directions need to be calculated every frame. This is perfect for Unity’s predefined Update function. There are many different parts to movement so there needs to be a few different variables defining and monitoring each aspect.
function Update() { forward = transform.forward; right = Vector3(forward.z, 0, -forward.x);   

The first two lines are assigning the direction that the character is facing forward and right. I will explain why we need these in a little bit. The second two statements create variables that will store input values. These values will constantly change based off if you are pressing the up/down keys (vertical) or right/left keys (horizontal). The “GetAxisRaw” method is used instead of the “GetAxis” because the latter applies smoothing which will make our rotations less responsive. Add the next few lines after the previous two. Keep everything inside the Update function.
                var targetDirection = horizontalInput * right + verticalInput * forward;   moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, 200 * Mathf.Deg2Rad * Time.deltaTime, 1000);   var movement = moveDirection * Time.deltaTime * 2; controller.Move(movement);
var horizontalInput = Input.GetAxisRaw("Horizontal"); var verticalInput = Input.GetAxisRaw("Vertical");

the first part creates a targetDirection variable that makes a direction that is currently being pressed. Your horizontalInput and verticalInput variables will return values between -1 and 1. They are the magnitudes of the direction. The right and forward variables return directions as vectors.
Since your character will be rotating when you press the left/right keys, its forward position will be changing a lot. Since the forward variable is changing based off your orientation, the verticalInput needs to know which direction is facing forward. That is why the targetDirection multiplies the forward and verticalInput variables.
The right variable value is dependent on the forward variable. It takes parts of the forward direction and can calculate the angle 90 degrees to the right of it. This gives a local x axis based off the rotation. That is why the horizontalInput is multiplied by the right direction.
This is a little conceptually heavy, so you might need to think about this for a little while.
The “moveDirection” variable stores a rotation value. It gets the current rotation and goes to the target rotation over so many seconds. The third parameter in the RotateTowards method is the rotation speed. Higher value equals means faster rotation.
The “movement” variable takes the direction and multiplies it by Time.deltaTime * 2. This will slow down the movement to rotate by seconds instead of by frames. The last line actually moves the character controller by the amount.
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

ly    

226

主题

1

听众

2755

积分

中级设计师

Rank: 5Rank: 5

纳金币
2755
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2011-12-23 20:34:14 |只看该作者
路过顶下吧、、、、
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-1-23 23:26:04 |只看该作者
得得失失平常事,是是非非任由之,恩恩怨怨心不愧,冷冷暖暖我自知,坎坎坷坷人生路,曲曲折折事业梯,凡事不必太在意,愿你一生好运气!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

地板
发表于 2012-2-11 23:18:37 |只看该作者
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-2-14 23:31:33 |只看该作者
谢谢楼主,真是太实用了
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-2-17 23:25:57 |只看该作者
佩服,好多阿 ,哈哈
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-2-21 23:18:24 |只看该作者
心中有爱,爱咋咋地
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-5-8 23:18:17 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-5-8 23:24:34 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2012-6-17 23:25:34 |只看该作者
好铁多多发,感激分享
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-13 16:38 , Processed in 0.553151 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部