c# - 如何检查物体是否在球体的顶部或底部碰撞
解决方案
一般来说,Vector没有位置。它只是空间中的一个方向和大小。位置基本上是一个向量本身,它从世界原点 (0,0,0) 指向相应的位置。因此,如果您只有一个方向矢量,则它不包含有关位置的任何信息。
您需要更好地定义顶部和底部,但如果您只想检查全局空间Y
,您可以通过比较它们的轴值来简单地检查碰撞接触点是高于还是低于球体中心。
像这样的东西
// Assuming this script is on the Sphere
private void OnCollisionEnter(Collision collision)
{
// You could of course take all contact points into account and take an average etc
// but to keep things simple just use only one for now
var contact = collision.GetContact(0);
var contactPoint = contact.point;
var ownCenter = contact.thisCollider.bounds.center;
if(contactPoint.y > ownCenter.y)
{
Debug.Log("Hit on top half");
}
else
{
Debug.Log("Hit on bottom half");
}
}
如果您想检查局部空间(例如,因为您的球体像图形中一样旋转),您可以使用几乎相同的方法,但只需先将位置转换为局部空间
var contactPoint = transform.InverseTransformPoint(contact.point);
// In the case for a sphere this should most probably be equal to simply using 0,0,0
// In case of a "SphereCollider" you could also use that specific type and then use "sphereCollider.center" which already is in local space
// But to have it working generally keep using the center for now
var ownCenter = transform.InverseTransformPoint(contact.thisCollider.bounds.center);
推荐阅读
- javascript - VueJS如何将v-model与数组的特定元素一起使用?
- c++ - C ++如何暂停控制台输出,以便用户在继续之前可以阅读信息
- python-3.x - 使用 mayavi 绘制 3D 网络时删除节点
- android - 如何有效地在一项活动中使用多个水平回收站视图?
- php - 为什么在 Elasticsearch 7 中创建索引时创建映射会导致文档被索引时映射更新被拒绝?
- ios - 为导航栏后退按钮和后退按钮文本设置不同的颜色
- react-native - 使导航可用于 React Native 应用程序中的所有组件
- python - os.environ python 如何获取默认用户环境变量
- sql - Postgres:如果我们多次选择一个计算列,Postgres 会一次又一次地计算它吗?
- javascript - 仅当关键字是单独的而不是另一个单词的一部分时,如何替换关键字?