查看: 3271|回复: 9
打印 上一主题 下一主题

unity3d第三人称角色控制教程-4(上)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

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

跳转到指定楼层
楼主
发表于 2011-12-8 17:19:28 |只看该作者 |倒序浏览


So far in this 3rd person character controller tutorial we have created a scene, placed a character, set the animation and input settings and applied a controller component so that Unity knows the character is player controlled. If you played the game you will see the character but you cannot move it around the game world, this is because we have not written a script to accept input and tell the character what to do, so we shall create that now.
Once we've finished this section the basic controller is complete, although there will be additional things we will want to do.
We will be stepping through the script in bits, don't worry if you get a bit lost, the entire code is provided at the bottom of this page if your not sure where to place some code, so just cross reference with the code paste at the end if you get lost.


Creating and Applying the Script



The first thing we need to do is to actually create a script, so in the Project Panel click the Create button and select "JavaScript", which will create a new JavaScript in the Project Panel.
Rename the script to "layerCharacter" and then drag it from the Project Panel onto the character entry in the Hierarchy in order to attach the script to the character. Now at this point anything in the script will be applied to the character we placed earlier.
Thats the easy part out of the way, now lets start scripting the controller behaviours, so open up the script in the editor and lets begin.


Writing the Script



If you've looked at any 3rd person controller scripts they are usually quite long and if your new to Unity they look quite complex, well they don't have to be, we'll also be stepping through script in sections to make it easy to understand whats going on.
Lets start by defining some important variables that the controller will use, so remove any default code already in the script so that its blank and then up at the top enter the following code (all of which is defining variables).
private var walkSpeed : float = 1.0;

private var gravity = 100.0;

private var moveDirection : Vector3 = Vector3.zero;

private var charController : CharacterController;
We've declared 4 variables above, so lets take a quick look at each.
- walkSpeed is a float (number with decimal) which will store the movement speed.

- gravity is another float which will contain the power of gravity. Gravity keeps the character on the ground.

- moveDirection is a Vector3 (a variable which holds x, y and z coordinates) which holds the direction the character will move in.

- charController is a character controller variable, we'll set the controller we added earlier to the variable so that we can reference it directly in the script.
Next we need to create a Start() function which will be called automatically when the game starts, this function will be used as an initialization routine.
The following code should be placed directly after the variable declarations.
function Start()

{

    charController = GetComponent(CharacterController);

    animation.wrapMode = WrapMode.Loop;

}
The Start() function is like a main() / winmain() function in C++ programming, its called automatically when the script it loaded. A Start() function is only called once however, it's not executed multiple times.
In the first line we are assigning the character controller we created earlier to that controller variable we set previously. The second line is setting the animation wrapMode to "loop". This is the scripting alternative to checking the loop box when we set each of the animations, by doing it like this we can get all animations to loop automatically.
Next we need an Update() function, which is another type of main function (like start()), the difference between start() and update() is that update() functions are called automatically once every frame, so its a good way to listen for events and script real time behaviours; of which the controller is. So below the Start() create an Update() function as follows:
function Update ()

{
}
All of our behaviour code will go in this function, so everything else is scripted in here. So save the script and we shall start to add code to control the character!



由 u8 发表



来源 unity3d8

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

315

主题

0

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
10878
精华
0

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

沙发
发表于 2011-12-9 09:11:39 |只看该作者
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

板凳
发表于 2012-2-14 23:22:30 |只看该作者
沙发不解释
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

地板
发表于 2012-3-9 23:23:31 |只看该作者
爱咋咋地!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

5#
发表于 2012-3-16 23:27:28 |只看该作者
水……生命之源……灌……
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-4-29 23:25:31 |只看该作者
凡系斑竹滴话要听;凡系朋友滴帖要顶!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

7#
发表于 2012-5-3 23:20:30 |只看该作者
呵呵,很好,方便罗。
回复

使用道具 举报

markq    

511

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
15839
精华
0

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

8#
发表于 2012-5-4 23:21:23 |只看该作者
  谢谢分享



爱生活 爱3D 爱纳金网



www.narkii.com
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

9#
发表于 2012-11-4 23:25:55 |只看该作者
你们都躲开,我来顶
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

10#
发表于 2013-3-21 23:24:50 |只看该作者
佩服,好多阿 ,哈哈
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

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

GMT+8, 2025-7-17 09:24 , Processed in 0.088553 second(s), 32 queries .

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

© 2008-2019 Narkii Inc.

回顶部