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("触发器触发");
    }
}
相关推荐
津津有味道1 小时前
Ntag 424 DNA写入URI网址配置开启动态UID计数器镜像C#源码
c#·uri·ndef·424dna·动态uid·计数器镜像
万19994 小时前
asp.net core webapi------3.AutoMapper的使用
c#·.netcore
唐青枫5 小时前
C#.NET 路由机制深入解析:从传统路由到 Endpoint Routing
c#·.net
TG:@yunlaoda360 云老大10 小时前
腾讯WAIC发布“1+3+N”AI全景图:混元3D世界模型开源,具身智能平台Tairos亮相
人工智能·3d·开源·腾讯云
心 爱心 爱10 小时前
Shape-Guided Dual-Memory Learning for 3D Anomaly Detection 论文精读
计算机视觉·3d·异常检测·工业异常检测·三维异常检测·多模态工业异常检测·二维异常检测
一步一个foot-print12 小时前
【Unity】Light Probe 替代点光源给环境动态物体加光照
unity·游戏引擎
@LYZY13 小时前
Unity 中隐藏文件规则
unity·游戏引擎·游戏程序·vr
hixiong12315 小时前
C# OpenCVSharp使用 读光-票证检测矫正模型
人工智能·opencv·c#
霜绛15 小时前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#
好望角雾眠15 小时前
第四阶段C#通讯开发-6:Socket之UDP
开发语言·笔记·学习·udp·c#