首页 技术 正文
技术 2022年11月20日
0 收藏 813 点赞 4,808 浏览 3044 个字

由于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;
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889