由于unity自带的碰撞组件特别耗费性能,网上的unity物体碰撞的c#代码实现比较少,没有适合的,只能自己写一个来用:

立方体:

using System;
using System.Collections.Generic;
using UnityEngine; namespace Assets
{
class Class1 : MonoBehaviour
{
List<Action> listAction = new List<Action>();
//用来检测碰撞的间隔时间(可以直接把检测放入update()中,不过100毫秒的定时器效果上也能实现)
System.Timers.Timer timer = new System.Timers.Timer();
//原坐标
Vector3 oldPoint;
//要移动的坐标
Vector3 point;
//0:待机;1:移动
int currentState = 0;
//是否可以移动
bool canMove = true; // Use this for initialization
void Start()
{
oldPoint = transform.position;
timer.Interval = 100;
timer.Enabled = true;
timer.Elapsed += (a, b) => isMove(point);
} // Update is called once per frame
void Update()
{
foreach (Action a in listAction) {
a();
}
listAction.Clear();
point = transform.position;
if (currentState == 1) {
if (checkCollision()) {
canMove = false;
}
}
} void isMove(Vector3 position) {
if (oldPoint != position)
{
if (!canMove)
{
listAction.Add(new Action(()=> gameObject.transform.position = oldPoint));
canMove = true;
}
else {
currentState = 1;
oldPoint = position;
}
}
else {
currentState = 0;
}
} bool checkCollision() {
Vector3 zzPoint = gameObject.transform.position;
Vector3 zzScale = gameObject.transform.localScale;
//另一物体坐标信息
GameObject dm = GameObject.Find("Cube (1)");
Vector3 dmPoint = dm.transform.position;
Vector3 dmScale = dm.transform.localScale; //坐标检测(当两个物体的x、y、z方向间距都小于两个物体在该方向上的长度)
if ((Math.Abs(zzPoint.x - dmPoint.x) <= zzScale.x / 2 + dmScale.x / 2) &&
(Math.Abs(zzPoint.y - dmPoint.y) <= zzScale.y / 2 + dmScale.y / 2) &&
(Math.Abs(zzPoint.z - dmPoint.z) <= zzScale.z / 2 + dmScale.z / 2))
{
return true;
}
return false;
}
}
}

球体:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class sphereCollision : MonoBehaviour { List<Action> listAction = new List<Action>();
//用来检测碰撞的间隔时间(可以直接把检测放入update()中,不过100毫秒的定时器效果上也能实现)
System.Timers.Timer timer = new System.Timers.Timer();
//原坐标
Vector3 oldPoint;
//要移动的坐标
Vector3 point;
//0:待机;1:移动
int currentState = 0;
//是否可以移动
bool canMove = true; // Use this for initialization
void Start()
{
oldPoint = transform.position;
timer.Interval = 100;
timer.Enabled = true;
timer.Elapsed += (a, b) => isMove(point);
} // Update is called once per frame
void Update()
{
foreach (Action a in listAction)
{
a();
}
listAction.Clear();
point = transform.position;
if (currentState == 1)
{
if (sphereCheckCollision())
{
canMove = false;
}
}
} void isMove(Vector3 position)
{
if (oldPoint != position)
{
if (!canMove)
{
listAction.Add(new Action(() => gameObject.transform.position = oldPoint));
canMove = true;
}
else
{
currentState = 1;
oldPoint = position;
}
}
else
{
currentState = 0;
}
} bool sphereCheckCollision()
{
Vector3 zzPoint = gameObject.transform.position;
Vector3 zzScale = gameObject.transform.localScale;
//另一物体坐标信息
GameObject dm = GameObject.Find("Sphere (1)");
Vector3 dmPoint = dm.transform.position;
Vector3 dmScale = dm.transform.localScale; //坐标检测(当两个圆相切时,圆心距离为r1+r2,两个圆心在x、y、z上的距离差为x1、x2、x3,用三角函数计算时至少计算两个维度的距离差才能和圆心距进行比较)
if ((Math.Sqrt(Math.Pow(Math.Abs(zzPoint.x - dmPoint.x), 2) + Math.Pow(Math.Abs(zzPoint.y - dmPoint.y), 2)) <= zzScale.x / 2 + dmScale.x / 2) &&
(Math.Sqrt(Math.Pow(Math.Abs(zzPoint.x - dmPoint.x), 2) + Math.Pow(Math.Abs(zzPoint.z - dmPoint.z), 2)) <= zzScale.x / 2 + dmScale.x / 2) &&
(Math.Sqrt(Math.Pow(Math.Abs(zzPoint.y - dmPoint.y), 2) + Math.Pow(Math.Abs(zzPoint.z - dmPoint.z), 2)) <= zzScale.x / 2 + dmScale.x / 2))
{
return true;
}
return false;
} }

unity3d立方体碰撞检测(c#代码实现)的更多相关文章

  1. unity3d 射弹基础案例代码分析

    #pragma strict import UnityEngine.UI; function Start () { } var speed : int = 5; var newobject : Tra ...

  2. Unity3D脚本--经常使用代码集

    1. 訪问其他物体 1) 使用Find()和FindWithTag()命令 Find和FindWithTag是很耗费时间的命令,要避免在Update()中和每一帧都被调用的函数中使用.在Start() ...

  3. Unity3D如何有效地组织代码?(转)

    问题: Unity3D可以说是高度的Component-Based Architecture,同时它的库提供了大量的全局变量.如何来组织代码呢? 答: - Unity有一些自身的约定,譬如项目里的Ed ...

  4. unity3d的碰撞检测及trigger

    A.基本概念 要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一 ...

  5. Unity3D如何有效地组织代码?

    本文整理自知乎,原文链接:http://www.zhihu.com/question/21070379 问题: Unity3D可以说是高度的Component-Based Architecture,同 ...

  6. unity3d随机地牢生成代码

    现在也是处于失业状态,碰巧看到个面试题是要用unity生成个随机地牢,就把做题过程中的思路和代码记录一下吧. 做完了以后我又想了一下,发现其实根本不需要这么麻烦,果然demo里的代码对我的思路影响还是 ...

  7. Unity3d ugui 实现image代码换图

    核心脚本代码 Image IMGE = transform.Find("IMGE").GetComponent<Image>();Sprite sprite1 = Re ...

  8. unity3d 场景配置文件生成代码

    using UnityEngine; using UnityEditor; using System.IO; using System; using System.Text; using System ...

  9. Unity3D安卓交互 - 使代码运行在UI线程

    runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub } });

随机推荐

  1. 【转载】java8中的Calendar日期对象(LocalDateTime)

    Java 8 推出了全新的日期时间API,Java 8 下的 java.time包下的所有类都是不可变类型而且线程安全. 下面是新版API中java.time包里的一些关键类: Instant:瞬时实 ...

  2. composer使用本地仓库

    { "repositories": { "sms": { "type": "path", "url" ...

  3. TCP/IP 第四、五章

    1, 2, 整个arp请求的过程. 3,arp -a 获取arp高速缓存.一般arp高速缓存存活时间20分钟,不完整的表项设置为3分钟.因为机器的ip地址可能发生改变. 4, 5,arp一般是操作系统 ...

  4. Ruby系列文章

    安装Ruby.多版本Ruby共存.Ruby安装慢问题 Ruby语言的一些杂项 Ruby中的常量:引号.%符号和heredoc Ruby中的数值 Ruby字符串(1):String基本用法 Ruby字符 ...

  5. HDU 5618:Jam's problem again(CDQ分治+树状数组处理三维偏序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意:…… 思路:和NEUOJ那题一样的.重新写了遍理解了一下,算作处理三维偏序的模板了. #includ ...

  6. 嵊州D2T1 “我只是来打个电话”

    嵊州D2T1 “我只是来打个电话” 精神病院有一个这样的测试. 给出一个正整数集合,集合中的数各不相同,然后要求病人回答: 其中有多少个数,恰好等于集合中另外两个(不同的)数之和? 回答正确的人,即可 ...

  7. SpringBoot整合Swagger和Actuator

    前言 本篇文章主要介绍的是SpringBoot整合Swagger(API文档生成框架)和SpringBoot整合Actuator(项目监控)使用教程. SpringBoot整合Swagger 说明:如 ...

  8. C++学习书籍推荐《The C++ Standard Library 2nd》下载

    百度云及其他网盘下载地址:点我 编辑推荐 经典C++教程十年新版再现,众多C++高手和读者好评如潮 畅销全球.经久不衰的C++ STL鸿篇巨著 C++程序员案头必 备的STL参考手册 全面涵盖C++1 ...

  9. MYSQL手工注入(详细步骤)—— 待补充

    0x00 SQL注入的分类: (1)基于从服务器接收到的响应         ▲基于错误的 SQL 注入         ▲联合查询的类型         ▲堆查询注射         ▲SQL 盲注 ...

  10. 上传文件不落地转Base64字符串

    1. 问题描述 因需调用第三方公司的图像识别接口,入参是:证件类型.图像类型.图片base64字符串,采用http+json格式调用. 本来采用的方式是:前端对图片做base64处理,后端组装下直接调 ...