角色控制器 CharacterController.Move

角色控制器没有碰撞效果和刚体最明显的区别

属性面板参数:

Slope Limit:爬坡限制
Step Offset:台阶高度
Skin Width :皮肤厚度 皮肤厚度决定了两个碰撞器可以互相渗入的深度。较大的皮肤厚值度会导致颤抖。小的皮肤厚度值会导致角色被卡住。一个合理的设定是使该值等于半径(Radius)的10%。
Min Move Distance:最小移动距离 如果角色移动的距离小于该值,那角色就不会移动。这可以避免颤抖现象。大部分情况下该值被设为0。
Center:中心点  该值决定胶囊碰撞器在世界空间中的位置,并不影响角色的行动。
Radius:半径 胶囊碰撞器的半径长度
Height:高度 角色的胶囊碰撞器高度

调用Move方法可以实现人物移动

以下为官方代码:

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

public class CubePlayer : MonoBehaviour
{
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;

    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);//移动
    }
}

注意的地方:

模拟重力的作用是当人物跳起或者从上往下可以落下来,不然人物不会往下走,不会触碰地面

猜你喜欢

转载自blog.csdn.net/i1tws/article/details/81104233