SURVIVAL SHOOTER TUTORIAL之为Player添加控制角色运动的脚本 [3]

基于上一篇文章的准备工作,在这篇文章中,为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跟随角色移动的脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值