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:新 ...
随机推荐
- 分布式领域CAP理论
分布式领域CAP理论,Consistency(一致性), 数据一致更新,所有数据变动都是同步的Availability(可用性), 好的响应性能Partition tolerance(分区容错性) 可 ...
- HTML中调用servlet的问题(?)
最近在学习servlet.刚开始时,按照案例一行一行的敲代码.不过,出问题了. hello1.html <!DOCTYPE html> <html> <head> ...
- [Reprint]c++ 析构函数的调用
析构函数在调用默认的析构函数和用户自己覆写的析构函数的时候有点意识模糊呢.写段代码总结下 #include <iostream> using namespace std; class Bo ...
- HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...
- Codeforces Round #288 (Div. 2)
A. Pasha and Pixels 题意就是给一个n*m的矩阵,k次操作,一开始矩阵全白,一次操作可以染黑一个格子,问第几次操作可以使得矩阵中存在一个2*2的黑色矩阵.直接模拟即可 代码: ...
- struts_19_对Action中所有方法、某一个方法进行输入校验(手工编写代码实现输入校验)
对所有方法进行校验1.通过手工编写代码的形式实现 需求:用户名:不能为空手机号:不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字 第01步:导包 第02步:配置web.xml <? ...
- Demo12SimpleAdapter
/Users/alamps/AndroidStudioProjects/Demo12SimpleAdapter/Demo12SimpleAdapter/src/main/res/layout/data ...
- JAVA测试装饰者模式
package shb.java.demo; /** * 测试装饰者模式 * @package :shb.java.demoJava02 * @author shaobn * @Describe : ...
- 解决无法连接到visual studio开发服务器的问题
今天vs抽风,调试网站出现下图那样: 然后我开始百度搜索 "无法连接到visual studio开发服务器" 出现很多文章: 打开一篇问题,看了里面的内容, 那个解决办法也不是最好 ...
- [Ubuntu] Ubuntu搭建VPN服务器pptpd
在 Ubuntu 上搭建 VPN 服务器的方法非常多,比较著名的有 PPTP, L2TP/IPSec 和 OpenVPN. 这三种方式中后两者的安全性比较好,但配置较麻烦.其中 OpenVPN 在 W ...