Unity胶囊体的碰撞检测实现

可选是否打开矩阵变换,支持xyz三种朝向
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class CapsuleDetection : MonoBehaviour
{
public enum Axis { X, Y, Z }
public Transform comparePointTransform;
public float radius;
public float height;
public Axis axis;
public bool enableMatrix; bool ExecuteDetection()
{
if (comparePointTransform == null) return false; var result = false;
var axisIndex = (int)axis; var comparePoint = comparePointTransform.position;
var pointA = Vector3.zero;
var pointB = Vector3.zero; if (enableMatrix)
{
comparePoint = transform.InverseTransformPoint(comparePoint);
pointA[axisIndex] -= height - radius;
pointB[axisIndex] += height - radius;
}
else
{
pointA = transform.position;
pointB = transform.position; pointA[axisIndex] -= height - radius;
pointB[axisIndex] += height - radius;
} if (comparePoint[axisIndex] < pointA[axisIndex])
{
if (Vector3.Distance(comparePoint, pointA) <= radius)
{
result = true;
}
} else if (comparePoint[axisIndex] > pointB[axisIndex])
{
if (Vector3.Distance(comparePoint, pointB) <= radius)
{
result = true;
}
}
else
{
var tmpPos = enableMatrix ? Vector3.zero : transform.position;
tmpPos[axisIndex] = comparePoint[axisIndex]; if (Vector3.Distance(comparePoint, tmpPos) <= radius)
{
result = true;
}
} return result;
} void OnDrawGizmos()
{
var color = Color.blue; if (ExecuteDetection())
{
color = Color.red;
} var axisIndex = (int)axis;
var pos1 = Vector3.zero;
var pos2 = Vector3.zero; if (enableMatrix)
{
Gizmos.matrix = transform.localToWorldMatrix;
pos1[axisIndex] -= height;
pos2[axisIndex] += height;
}
else
{
pos1 = transform.position;
pos2 = transform.position; pos1[axisIndex] -= height;
pos2[axisIndex] += height;
} DebugExtension.DrawCapsule(pos1, pos2, color, radius);
}
}
绘制胶囊体使用DebugExtension插件,在资源商店可以免费下载
Unity胶囊体的碰撞检测实现的更多相关文章
- UE4.11新特性:胶囊体阴影
官方介绍 虚幻引擎现在支持非常柔滑的间接阴影,由代表角色的胶囊体来进行投影. 通常,在受间接光照时,并不会产生阴影,除非是屏幕空间环境遮罩.间接投影需要做的非常柔滑,因为间接光照是来自很多不同的方向, ...
- [Unity 3D] Unity 3D 里的碰撞检测
Unity 3D里两个碰撞体之间发生碰撞可以用OnCollision族函数和OnTrigger族函数来获知和处理.Unity官方给出了两张可发生碰撞的组合表: Collision detection ...
- Kinect+unity 实现体感格斗闯关小游戏
文章目录 项目地址 1 项目概况 1.1 项目简介 1.2 项目目的 1.3 主要技术 2 设计 2.1 基本概念 2.2 框架 2.3 算法 2.4 模型 2.5 调查问卷 3 实现 3.1 技术难 ...
- 【Unity入门】碰撞检测与触发检测
版权声明:本文为博主原创文章,转载请注明出处. 在Unity里面,游戏物体的碰撞我们可以通过刚体组件(Rigidbody)和碰撞器组件(Collider)来进行检测.首先在场景里面添加一个Plane面 ...
- Unity基础知识学习笔记二
1,object Instantiate(object original,Vector3 position,Quaternion rotation) 克隆原始物体,并返回克隆物体. ...
- 关于Unity中ARPG游戏人物移动(专题十一)
ARPG:动作型角色扮演类游戏 大多数的ARPG游戏都是使用摇杆操作,以第三人称摄像机的方式来跟随主角,实际上人物只走八个方向,上,下,左,右,左上,左下,右下,右上 控制角色移动的思路1: 在ARP ...
- 【Unity】11.2 刚体(Rigidbody)
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 Rigidbody(刚体)组件可使游戏对象在物理系统的控制下来运动,刚体可接受外力与扭矩力,使游戏对象像在真实世界中那样 ...
- 【Unity】11.1 角色控制器 (Character Controller)
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 角色控制器(Character Controller)主要用于对第三人称或第一人称游戏主角的控制.如果要创建类人角色,可 ...
- 【Unity】2.2 Unity编辑器中的常用菜单项
分类:Unity.C#.VS2015 创建日期:2016-03-26 Unity 5.3.4编辑器共提供了7个主菜单项,这一节主要学习其中的常用项. 一.File 1.基本功能 New Scene:新 ...
随机推荐
- tinyxml安装和使用
下载: 官方文档:http://www.grinninglizard.com/tinyxmldocs/index.html 中文翻译:http://www.cnblogs.com/kex1n/arch ...
- switch为什么不能用string类型?
switch()括号里面的参数是一个int型值啊 你要可以转换为int型的参数才行得通啊
- F面经:painting house
There are a row of houses, each house can be painted with three colors red, blue and green. The cost ...
- Java基础(57):Eclipse中环境配置(视图字体颜色行号调试快捷键等等)
1:Eclipse的基本配置 A:程序的编译和运行的环境配置(一般不改) window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本. 运行环境:I ...
- 安装Git
因为我的开发环境是Windows,那么在windows下安装Git就简单很多了.首先在,从http://msysgit.github.io/下载Git的安装程序,然后按默认选项安装即可. 安装成功之后 ...
- Envelope对象介绍
Envelope也称包络线,是一个矩形区域,是每个几何形体的最小外接矩形.每个Geometry都拥有一个Envelope,包括Envelope自身. 它定义了XMax,XMin,YMax,YMin,H ...
- java socket 发送文件
客户端: package tt; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStrea ...
- js默认比较第一个数字大小
解决办法:先把数字用Number转化:num=Number(num);然后再比较 和parseInt区别
- opencv载入,显示及保存图像
1.声明一个表示图像的变量,在OpenCV2中,这个变量是cv::Mat类型,该类是用于保存图像以及其他矩阵数据的数据结构.默认情况下它们的尺寸为0. cv::Mat image; // ...
- kernel32.dll出错解决方案
kernel32.dll 一.什么是kernel32内核文件 kernel32.dll是Windows 9x/Me中非常重要的32位动态链接库文件,属于内核级文件.它控制着系统的内存管理.数据的输入输 ...