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

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

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

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

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


Creating the Basic Behaviour Code



Firstly, we don't want the character to be able to walk or turn whilst in mid air (if the character were to step off a ledge for example, the character wouldnt walk forward or turn), so we need to detect whether the character is grounded, if the character is grounded we allow the player to move the character, else the character will just fall until it touches the ground.
So add the following IF statement at the top of the Update() function:
if(charController.isGrounded == ***e)

{
}
What this code is doing is saying if the Character Controller component (which we are referencing using the charController variable) is touching the ground we can perform any behaviours within the IF statement. "isGrounded" is a condition which Unity automatically understands, simply put if the character controller is touching the ground then isGrounded will return ***e, if the character is not touching the ground, isGrounded will return false.
If the character is not grounded we need to tell Unity to bring the character down to the ground, so we want to add some code after the IF statement to apply the affect of gravity on the player character.
So, below the IF statement, enter the following code:
moveDirection.y -= gravity * Time.deltaTime;
This is applying the affect of gravity by setting the Y attribute of the Vector3 variable (moveDirection) to the power of gravity every second, which will make the character fall, or at least it would if we had a line of code which would apply the value of MoveDirection onto the character.
Place the following code below the gravity code:
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
This new line of code is moving the character using the "Move()" function, which is a reserved Unity function for moving objects. We are passing the moveDirection Vector3 variable to the function; which holds X, Y and Z position values and applying them based on time * the speed of movement.
In other words, we're moving the character in the direction specified in the moveDirection variable (up, down, left, right, forward, backward) at the speed defined by walkSpeed every second.
If you save the script and raise the character using the translate tool so its up above the ground you will see that the character now falls until it touches the ground. If your character falls through the ground, then raise it until its feet are on or above the ground. If the character falls through the ground when its positioned correctly its because the controller component capsule you scaled earlier is not the same size as the character and the bottom of it is not in line with the characters feet.
So we have gravity applied, lets add some code so the character can walk forwards. To do this we need to detect input on the Vertical axis and tell the character to move forwards if there is a positive input.
Add the following code into the isGrounded IF statement:
if(Input.GetAxis("Vertical") > .1)

{

}
We are using the Input.GetAxis("name") to read in any input on the Vertical axis; if the input value is greater than 0.1 then we have a forward input request. The reason Axes have a *.* value is because of analogue controllers where you may want to read in a range (i.e. controlling speed by how far the analogue stick is pushed). We can also detect reverse (backwards) motion by listening for a negative value on this axis.
Anyhow, if the forward button is pressed the code inside this IF statement will***cute.
Now before we tell the character to move forward, we have a walk***n toggle which switch between walking and***nning, basically if CTRL is pressed then the character will***n, otherwise the character will walk. If you only have one walk speed then you can skip this and just enter the code for moving the character, but most games have two movement speeds at least, so lets create another IF statement which will detect whether we should be***nning or walking.
Place the following code into the GetAxis("Vertical") if statement:
if(Input.GetButton("Run"))

{
}

else

{
}
This new IF statement listens for the Run button to be pressed using the GetButton (which has an on/off value). If the Run button is pressed then we do the code to make the character***n, else we do the code to make the character walk.
So lets add the walk and***n code into this IF statement.
animation.CrossFade(&quot***n");

walkSpeed = 4;
The code above goes into the first condition of the IF statement ***n). The first line uses animation.CrossFade("animation_name") to blend into the requested animation, being the***n animation. The second line is setting the walk speed to 4 ***nning speed).
We then want the following code in the else statement:
animation.CrossFade("walk");

walkSpeed = 1;
This is doing the same as above, but blending into the walk animation and setting the speed to 1 (walking speed).
Next we want to add some code to return the animation cycle to idle if the forward axis is released.
else

{

    animation.CrossFade("idle");

}
This ELSE statement wants to go directly after the IF where we detect the vertical axis, so it should read as IF(Input.GetAxis("Vertical") we do the forward movement, ELSE we do the idle bit.
So now we are detecting forward motion,***n, walk speeds and idle states. We now want to detect backward motion, so add the following code to the IF statement, between the Vertical and the idle blocks:
else if(Input.GetAxis("Vertical") < -.1)

{

    animation["walk"].speed = -1;

    animation.CrossFade("walk");

    walkSpeed = 1;

}
This code is detecting whether the vertical axis is recieving a negative value (which would be backwards) and then setting the animation speed to -1 (which plays the walk animation in reverse), blends the animation to walk and sets the speed to 1.
If you save the script and hit play now, you will be able to walk the character forward and backward and switch between walking and***nning, however while the character is animated it won't actually move forward or backwards yet.
Place the following two lines of code at the bottom of the IsGrounded IF statement, they should be the last two lines of code in the IF:
moveDirection = Vector3(0,0, Input.GetAxis("Vertical"));

moveDirection = transform.TransformDirection(moveDirection);
These two lines of code will set the vertical movement and movement direction (which we will implement in a moment) so that the character will actually move.
While we can walk and***n forwards and backwards we cannot turn yet, we need to add some code to detect the horizontal axis and convert this into a turn; add the following code right before the two lines we just added:
transform.eulerAngles.y += Input.GetAxis("Horizontal");
This code will set the rotation direction to the Horizontal axis, which creates the turn. Hit play now and the character can turn, however you might notice two quirks:
1. If you walk backwards the walk cycle will continue to play backwards when you walk forward again

2. If you turn on the spot without moving the character doesnt step.
Lets fix both of these issues now:
Add the following code to the top of the walk IF statement:

animation["walk"].speed = 1;
This will ensure that the walk cycle always plays forward if your walking forward.
Then this block below the IsGrounded IF statement (before the line where we set the rotation eulerAngles to Y:
// Create an animation cycle for when the character is turning on the spot

if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))

{

    animation.CrossFade("walk");

}
This IF statement will detect if the character is turning while not moving forward (no vertical input) and play the walk cycle animation while turning.
Now if you save the completed script and play the game you'll have full forward, backward, walk,***n and turning control over the character and it shouldn't have any major quirks (beyond the roughness of our test characters animation).
So with that done its time to make the camera follow the character, the scripting side of things is completed for now, so close the script and return to the Unity editor and continue to part 5 to make the camera follow 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-6 23:35:32 |只看该作者
呵呵,真得不错哦!!
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

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

地板
发表于 2012-2-7 14:12:16 |只看该作者
这么牛
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

5#
发表于 2012-2-17 23:28:25 |只看该作者
提醒猪猪,千万不能让你看见
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-4-16 23:20:09 |只看该作者
佩服,好多阿 ,哈哈
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

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

7#
发表于 2012-4-28 23:19:50 |只看该作者
人过留名!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

8#
发表于 2012-4-28 23:21:27 |只看该作者
不错哦,顶一下......
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

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

9#
发表于 2012-4-29 23:18:59 |只看该作者
有意思!学习了!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2012-5-12 23:24:36 |只看该作者
水……生命之源……灌……
回复

使用道具 举报

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

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

GMT+8, 2025-7-17 21:50 , Processed in 0.070626 second(s), 32 queries .

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

© 2008-2019 Narkii Inc.

回顶部