【Unity】多种方法实现第一人称角色移动(一)角色控制器CharacterController

前言

  在Unity中要实现第一人称视角移动的方法有很多,每种方法各有优劣,本次要介绍的就是使用角色控制器CharacterController来实现的方法。

  在阅览下面的步骤之前,你首先需要一个第一人称视角的实体。

  最简单的第一视角实体只需要一个Capsule和一个摄像机,就像这样:

  

  这样,你就获得了一个最简单的人和一双能看见世界的眼睛(虽然没有四肢)


步骤一、使用角色控制器CharacterController实现角色的移动

 接下来只要让它能动起来就达到了我们的目的。所以我们要给它绑定上角色控制器CharacterController组件

什么是角色控制器CharacterController

 角色控制器CharacterController,是Unity提供的可以实现移动的组件

 调用CharacterController下的Move()方法即可以实现最简单的人物移动。

 同时,CharacterController下的isGrounded属性可以检测当前人物是否在地面上。


    

 组件参数解释:

  Slope Limit        爬坡限制:小于或等于此角度时可以上坡
  Step Offset        台阶高度
  Skin Width        皮肤宽度
  Min Move Distance    最小移动距离
  Center          中心点坐标
  Radius          半径
  Height          高度


 简单的物体移动代码示例:

using UnityEngine;
using System.Collections;
 
public class NewBehaviourScript : MonoBehaviour
{
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;

    void Start()
    {
        float _horizontal = Input.GetAxis("Horizontal");
        float _vertical = Input.GetAxis("Vertical");
    }
    void Update()
    {
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }

}

 注意事项:

  角色控制器CharacterContriller和刚体不同,它没有碰撞效果,不可以像刚体一样对齐施加一个力。



步骤二、添加摄像机视角旋转代码

 添加上角色控制器组件和代码之后,人物就可以随着我们键盘WASD进行移动了。

 但是只会前后移动不会转身的人物可算不上是第一人称视角移动。因此我们需要在角色控制的代码基础上添加使摄像机视角旋转的代码。

 通过GetAxis()获取鼠标的位移

Input.GetAxis("Mouse X");
Input.GetAxis("Mouse Y");

 限定上下视角旋转的范围,(因为正常的人不可能纵向观察360度,但是我们可以通过转身观察到横向360度)

float RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);

 需要注意的是,只有摄像机跟随视角旋转是不足够的,还需要让人物本身也一起旋转。

this.transform.eulerAngles = new Vector3(0, RotationX, 0);
gretctCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0);

 最后绑定在人物上的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMoveController : MonoBehaviour
{
    CharacterController playerController;

    Vector3 direction;

    public float speed = 1;
    public float jumpPower = 5;
    public float gravity = 7f;

    public float mousespeed = 5f;


    public float minmouseY = -45f;
    public float maxmouseY = 45f;

    float RotationY = 0f;
    float RotationX = 0f;

    public Transform agretctCamera;


    // Use this for initialization
    void Start()
    {
        playerController = this.GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        float _horizontal = Input.GetAxis("Horizontal");
        float _vertical = Input.GetAxis("Vertical");

        if (playerController.isGrounded)
        {
            direction = new Vector3(_horizontal, 0, _vertical);
            if (Input.GetKeyDown(KeyCode.Space))
                direction.y = jumpPower;
        }
        direction.y -= gravity * Time.deltaTime;
        playerController.Move(playerController.transform.TransformDirection(direction * Time.deltaTime * speed));

        RotationX += agretctCamera.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * mousespeed;

        RotationY -= Input.GetAxis("Mouse Y") * mousespeed;
        RotationY = Mathf.Clamp(RotationY, minmouseY, maxmouseY);
    
        this.transform.eulerAngles = new Vector3(0, RotationX, 0);

        agretctCamera.transform.eulerAngles = new Vector3(RotationY, RotationX, 0);

    }
}
原文地址:https://www.cnblogs.com/Neko-YG/p/14810409.html