unity3d立方体碰撞检测(c#代码实现)
由于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#代码实现)的更多相关文章
- unity3d 射弹基础案例代码分析
#pragma strict import UnityEngine.UI; function Start () { } var speed : int = 5; var newobject : Tra ...
- Unity3D脚本--经常使用代码集
1. 訪问其他物体 1) 使用Find()和FindWithTag()命令 Find和FindWithTag是很耗费时间的命令,要避免在Update()中和每一帧都被调用的函数中使用.在Start() ...
- Unity3D如何有效地组织代码?(转)
问题: Unity3D可以说是高度的Component-Based Architecture,同时它的库提供了大量的全局变量.如何来组织代码呢? 答: - Unity有一些自身的约定,譬如项目里的Ed ...
- unity3d的碰撞检测及trigger
A.基本概念 要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一 ...
- Unity3D如何有效地组织代码?
本文整理自知乎,原文链接:http://www.zhihu.com/question/21070379 问题: Unity3D可以说是高度的Component-Based Architecture,同 ...
- unity3d随机地牢生成代码
现在也是处于失业状态,碰巧看到个面试题是要用unity生成个随机地牢,就把做题过程中的思路和代码记录一下吧. 做完了以后我又想了一下,发现其实根本不需要这么麻烦,果然demo里的代码对我的思路影响还是 ...
- Unity3d ugui 实现image代码换图
核心脚本代码 Image IMGE = transform.Find("IMGE").GetComponent<Image>();Sprite sprite1 = Re ...
- unity3d 场景配置文件生成代码
using UnityEngine; using UnityEditor; using System.IO; using System; using System.Text; using System ...
- Unity3D安卓交互 - 使代码运行在UI线程
runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub } });
随机推荐
- 机器学习-5 支持向量机SVM
一.概念和背景 SVM:Support Vector Machine 支持向量机. 最早是由Vladimir N. Vapnik和Alexey Ya. Chervonenkis在1963年提出的. 目 ...
- ZooKeeper —— 单机环境和集群环境搭建
一.单机环境搭建 1.1 下载 下载对应版本Zookeeper,这里我下载的版本3.4.14.官方下载地址:https://archive.apache.org/dist/zookeeper/ # w ...
- Consul&Nginx&Registrator&ConsulTemplate部署高可用负载均衡
1. Consul Server 创建consul server虚拟主机 docker-machine create consul 出现如下内容即创建成功 Running pre-create che ...
- Java学习笔记——设计模式之八.外观模式
外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 子系统: package cn.happy.design_patter ...
- 01-Javascript基础
一. JS介绍 JavaScript是前台语言 JavaScript是前台语言,而不是后台语言. JavaScript运行在用户的终端网页上,而不是服务器上,所以我们称为“前台语言”. JavaScr ...
- 用python的matplotlib和numpy库绘制股票K线均线的整合效果(含从网络接口爬取数据和验证交易策略代码)
本人最近在尝试着发表“以股票案例入门Python编程语言”系列的文章,在这些文章里,将用Python工具绘制各种股票指标,在讲述各股票指标的含义以及计算方式的同时,验证基于各种指标的交易策略,本文是第 ...
- Codeforces Gym100543L:Outer space invaders(区间DP)
题目链接 题意 有n个人,每个人有一个出现时间a和一个开枪时间b和一个距离d,在任意一个时刻,你可以选择炸人,你要炸一个人的花费是和他的距离d,并且所有的已经出现并且还没开枪的和你距离<=d的人 ...
- CSDN,CNBLOGS博客文章一键转载插件(转载测试)
插件地址: https://greasyfork.org/zh-CN/scripts/381053-csdn%E5%8D%9A%E5%AE%A2%E6%96%87%E7%AB%A0%E8%BD%AC% ...
- django基础知识之布署:
布署 从uwsgi.nginx.静态文件三个方面处理 服务器介绍 服务器:私有服务器.公有服务器 私有服务器:公司自己购买.自己维护,只布署自己的应用,可供公司内部或外网访问 公有服务器:集成好运营环 ...
- BeanUtils.copyProperties的用法
实现原理 原理 target.set + source的属性名(source.get + source的属性名):所有source必须有get方法,target必须有set方法 一. springfr ...