纳金网

标题: unity3d第三人称角色控制教程-4(上) [打印本页]

作者: 会飞的鱼    时间: 2011-12-8 17:19
标题: unity3d第三人称角色控制教程-4(上)


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


作者: osaka    时间: 2011-12-9 09:11

作者: 奇    时间: 2012-2-14 23:22
沙发不解释

作者: 奇    时间: 2012-3-9 23:23
爱咋咋地!

作者: tc    时间: 2012-3-16 23:27
水……生命之源……灌……

作者: 晃晃    时间: 2012-4-29 23:25
凡系斑竹滴话要听;凡系朋友滴帖要顶!

作者: C.R.CAN    时间: 2012-5-3 23:20
呵呵,很好,方便罗。

作者: markq    时间: 2012-5-4 23:21
  谢谢分享



爱生活 爱3D 爱纳金网



www.narkii.com
作者: C.R.CAN    时间: 2012-11-4 23:25
你们都躲开,我来顶

作者: 奇    时间: 2013-3-21 23:24
佩服,好多阿 ,哈哈





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