当前位置:网站首页>【Unity3D】角色控制器(CharacterController)
【Unity3D】角色控制器(CharacterController)
2022-07-29 11:31:00 【little_fat_sheep】
1 简介
控制角色移动的组件主要有:Transform 组件、Rigidbody 组件、CharacterController 组件。Transform 组件通过控制角色位置实现移动,Rogidbody 组件通过控制角色速度实现移动,CharacterController 组件通过控制角色速度和模拟碰撞实现移动。Transform 组件和 Rogidbody 组件不便于控制角色爬坡和爬梯,CharacterController 组件能够很方便地实现角色在各种复杂地形中运动。
CharacterController 继承 Collider(见→碰撞体组件Collider),其属性面板如下:
- Slope Limit:爬坡最大角度
- Step Offset:爬梯最大高度
- Skin Width:皮肤厚度
- Min Move Distance:最小移动距离
- Center、Radius、Height:角色用于检测碰撞的胶囊体中心、半径、高
说明:CharacterController 继承 Collider,并且其碰撞体是一个胶囊体。
CharacterController 中控制角色移动的方法如下:
public CollisionFlags Move(Vector3 motion)
public bool SimpleMove(Vector3 speed)
2 应用
1)游戏对象
创建 2 个楼梯(每个楼梯由 5 个 Cube 对象拼成)和 2 个斜坡以及一个胶囊体,如下:
2)脚本组件
PlayerController.cs
using UnityEngine;
public class PlayerController : MonoBehaviour {
private CharacterController character;
private float speedRate = 4f;
private void Start () {
character = GetComponent<CharacterController>();
}
private void Update () {
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 speed = new Vector3(hor, 0, ver) * speedRate;
character.SimpleMove(speed);
}
}
说明: PlayerController 脚本组件挂在胶囊体上。
3)运行效果
边栏推荐
- [SwiftUI 开发] @State @Binding @ObservedObject @EnvironmentObject
- 从零开始Blazor Server(3)--添加cookie授权
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
- 基本.分块
- QT's user-defined interface (borderless and movable)
- Mall mall based on flask --- user module
- How to use "copy – link" to accelerate docker to build and optimize cache
- ️ 炒 股 实 战丨原 地 起 飞 ️
- Learn weekly - 64 - a v2ex style source BBS program
猜你喜欢
MySql启动、连接sqlog、主从复制、双机热备(开机时)
Lucky draw system with background source code
共建共享数字世界的根:阿里云打造全面的云原生开源生态
Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life
2022最新 wifi大师小程序独立版3.0.8
Based on the flask to write a small shopping mall project
什么是 Kubernetes 自定义资源定义 (CRD)?
惠及6亿人 投资98亿 沿江高铁武宜段最新进展来了!
DNS protocol, ICMP protocol, NAT technology
MyCat中间件高可用、读写分离、分片、主从切换、ER分片
随机推荐
「PHP基础知识」使用数组保存数据
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主
谷歌“消灭” Cookie 计划延至 2024 年
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
Applied practical skills of deep reinforcement learning
暑假集训week1
PHP basics uses arrays to save data
如何使用 grep 跨多行查找模式匹配
如何在匹配行之前使用 grep 显示文件名和行号
【Untitled】
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
Qt 之自定义界面(实现无边框、可移动)
ES6 arrow function this points to
Std:: vector copy, append, nested access
微信云托管入门与实践
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
"Knowledge Collection" article to understand mysql index!!(recommended collection)
INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
Talk about the establishment of performance testing environment
Deep understanding of c # delegate into the fast lanes