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:新 ...
随机推荐
- 实验十三_编写、应用中断例程_2 & 总结
编写并安装int 7ch中断例程,功能为完成loop指令的功能 参数:(cx)= 循环次数,(bx)= 位移 以上中断例程安装成功后,对下面的程序进行单步跟踪,尤其注意观察int.iret指令执行前后 ...
- Good Bye 2013
C:有点这种题的经验,先存起来相等的 D:赛后还搓了好久的代码,其实长度就100,枚举两边情况,其实A和C就涵盖了所有情况!所以到2就可以了,而且我弄出了有多少个后,和两边情况,也不知道能否或怎么凑成 ...
- .NET: WPF DependencyProperty
DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...
- poj: 3094
简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...
- ini,config文件的读取修改
修改ini配置文件 // 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport(&qu ...
- Sqlserver列出所有数据库名,表名,字段名
Sqlserver列出所有数据库名,表名,字段名 1.获取所有数据库名: ? 1 SELECT Name FROM Master..SysDatabases ORDER BY Name 注 ...
- python2.7使用ansible
升级python到2.7后 $ ansible 报错 Traceback (most recent call last): File "/usr/bin/ansible", lin ...
- System Hold, Fix Manager before resetting counters
程序pending http://www.askmaclean.com/archives/2011/11 http://blog.itpub.net/35489/viewspace-717132/ 1 ...
- 8007003Windows Update遇到未知错误
如果在检查更新时收到 Windows Update 错误 80070003,则需要删除 Windows 用于标识计算机更新的临时文件.若要删除临时文件,请停止 Windows Update 服务,删除 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON color_fuses2
zw版[转发·台湾nvp系列Delphi例程]HALCON color_fuses2 procedure TForm1.Button1Click(Sender: TObject);var w, h : ...