基于上一篇文章的准备工作,在这篇文章中,为Player Character添加控制角色运动的脚本PlayerMovement。在脚本中,分别写了控制角色移动的函数、控制角色旋转以及设置角色启用动画的函数。
1. 控制角色移动的函数
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// 变量
Vector3 movement;
public float speed;// 角色移动速度
void Awake() {
}
void FixedUpdate() {
// Store the input axes.
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
}
// 移动函数 Move the player around the scene.
void Move(float h, float v) {
movement.Set(h, 0f, v); // movement = new Vector3(h, 0.0f, v);
movement = movement.normalized * speed * Time.deltaTime; // Normalise the movement vector and make it proportional to the speed per second.
GetComponent<Rigidbody>().MovePosition(transform.position + movement); // Move the player to it's current position plus the movement.
// MovePosition: move the body to the position of transform.position + movement
}
// 旋转
void Turning() {
}
// 启用动画
void Animat() {
}
}
效果如下图:
2. 控制角色旋转的函数
怎样使用射线?可参考这篇关于Unity3D射线的原理及其用法的文章。
代码清单如下:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// 变量
Vector3 movement;
public float speed;// 角色移动速度
private float camRayLength = 100f;
int floorMask;
void Awake() {
floorMask = LayerMask.GetMask("Floor");
}
void FixedUpdate() {
// Store the input axes.
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
//Turning();
}
// 移动函数 Move the player around the scene.
void Move(float h, float v) {
movement.Set(h, 0f, v); // movement = new Vector3(h, 0.0f, v);
movement = movement.normalized * speed * Time.deltaTime; // Normalise the movement vector and make it proportional to the speed per second.
GetComponent<Rigidbody>().MovePosition(transform.position + movement); // Move the player to it's current position plus the movement.
// MovePosition: move the body to the position of transform.position + movement
}
// 转身
void Turning() {
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);// Create a ray from the mouse cursor on screen in the direction of the camera.
RaycastHit floorHit;
if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion rot = Quaternion.LookRotation(playerToMouse);
// 控制角色旋转
GetComponent<Rigidbody>().MoveRotation(rot);
}
}
// 启用动画
void Animat() {
}
}
注意:要把MainCamera中Tag设置为MainCamera,不然Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);会发生异常,角色不会面向鼠标转身!
效果如下图:
3. 控制角色动画的函数
void Animating(float h, float v) {
GetComponent<Animator>().SetBool("IsWalking", h != 0f || v != 0f);
}
在这句代码中, GetComponent<Animator>()为获取绑定了该脚本的角色的动画状态机,SetBool函数中第一个参数是状态机中的Trigger触发器,第二个参数表示如果为真,即角色发生移动,那么就把IsWalking设置为true,角色就会执行运动时的动画,否则不执行运动动画,处于Idle状态。
4. PlayerMovement脚本完整代码
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// 变量
Vector3 movement;
public float speed;// 角色移动速度
private float camRayLength = 100f;
int floorMask;
void Awake() {
floorMask = LayerMask.GetMask("Floor");
}
void FixedUpdate() {
// Store the input axes.
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
Turning();
Animating(h, v);
}
// 移动函数 Move the player around the scene.
void Move(float h, float v) {
movement.Set(h, 0f, v); // movement = new Vector3(h, 0.0f, v);
movement = movement.normalized * speed * Time.deltaTime;
// Normalise the movement vector and make it proportional to the speed per second.
GetComponent<Rigidbody>().MovePosition(transform.position + movement);
// Move the player to it's current position plus the movement.
// MovePosition: move the body to the position of transform.position + movement
}
// 转身
void Turning() {
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if (Physics.Raycast(camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion rot = Quaternion.LookRotation(playerToMouse);
// 控制角色旋转
GetComponent<Rigidbody>().MoveRotation(rot);
}
}
// 启用动画
void Animating(float h, float v) {
GetComponent<Animator>().SetBool("IsWalking", h != 0f || v != 0f);
}
}
效果如下图:
5. 总结
① 在上篇文章中,
角色能够运动,包括:移动、转身以及动画动作;
②
MainCamera是静止的,很多游戏中相机是跟随角色移动而移动,这将在下篇文章中编写
Camera跟随角色移动的脚本。