Unity-角色控制器

角色控制器是什么?

角色控制器是让角色可以受制于碰撞 但是不会被刚体所牵制

如果我们对角色使用刚体判断碰撞,可能会出现一些奇怪的表现

比如:

  1. 在斜坡上往下滑动

  2. 不加约束的情况碰撞可能让自己被撞飞

    等等

    而角色控制器会让角色表现的更加稳定

    Unity 提供了角色控制器脚本专门用于控制角色

注意:

添加角色控制器后,不用再添加刚体

能检测碰撞函数

能检测触发器函数

能被射线检测

Slope Limit坡度度数限制,大于该值的斜坡上不去

Step Offset:台阶偏移值,单位为米,低于这个值的台阶才能上去,该值不能大于角色控制器的高度

Skin Width:皮肤的宽度,两个碰撞体可以穿透彼此最大的皮肤宽度,较大的值可以减少抖动,较小的值可能导致角色卡住,建议设置为半径的 10%

MinMoveDistance:最小移动距离,大多数情况下为 0,可以用来减少抖动

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class lesson19 : MonoBehaviour
{

    private Animator animator;
    private CharacterController cc;
    private float h;
    private float v;
    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        cc = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        //判断是否接触地面
          if (cc.isGrounded)
          {
        print("接触地面");
          }
        animator.SetFloat("x", h);
        animator.SetFloat("y", v);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("New Trigger");
        }
        //受重力作用的移动
        //cc.SimpleMove(transform.forward * v * 180 * Time.deltaTime);
        cc.SimpleMove(new Vector3(h, 0, v) * 3);
        //不受重力影响的移动
        //cc.Move();
    }
    //碰撞到其他对象触发
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        print(hit.collider.gameObject.name);
    }

    private void OnTriggerEnter(Collider other)
    {
        print("触发器触发");
    }
}
相关推荐
esmap37 分钟前
ESMAP 智慧消防解决方案:以数字孪生技术构建全域感知消防体系,赋能消防安全管理智能化升级
人工智能·物联网·3d·编辑器·智慧城市
Yorlen_Zhang1 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
不绝1911 小时前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
zhooyu2 小时前
C++和OpenGL手搓3D游戏编程(20160207进展和效果)
开发语言·c++·游戏·3d·opengl
大鹏说大话2 小时前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
czhc11400756633 小时前
通信 28
c#
我的offer在哪里4 小时前
示例 Unity 项目结构(Playable Game Template)
unity·游戏引擎
bugcome_com6 小时前
C# 程序结构详解:从 Hello World 开始
c#
淡海水7 小时前
【节点】[Branch节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·branch
唐梓航-求职中7 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#