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