- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
 
- 纳金币
- -1
- 精华
- 11
|
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
|
|