C# 我们能用物理方法检测刚体碰撞的时间吗 public void运行模拟() { 物理。自动模拟=错误; SimulatedBody=FindObjectSoftType()。选择(rb=>new SimulatedBody(rb)).ToArray(); striker.GetComponent().AddForce(新矢量3(200.0f、0.0f、200.0f)); 对于(int i=0;ibody.rigidbody.IsSleeping()) { 印刷品(一); 打破 } } Physics.autoSimulation=true; }
我试着用物理来统一模拟。根据unity文档,“模拟包括碰撞检测、刚体和关节集成以及物理回调(接触、触发和关节)归档的所有阶段。” 我需要知道我的striker游戏对象与其他游戏对象在哪个迭代中发生碰撞。如果我使用自动模拟,OnCollisionCenter可以工作。但是当我使用物理时它不工作。模拟它不注册。我做错了什么,或者当我的对象与其他对象碰撞时我如何获得 我们能用物理方法检测刚体碰撞的时间吗 统一 是的,它可以 我曾经对此做过一个实验,它成功了,我打开了使用的同一个示例场景,再次进行了测试,它再次成功了。下面是文档中的一个声明,也是您的问题: 仿真包括碰撞检测的所有阶段,刚体 和关节整合,并归档物理回调 (触点、触发器和接头) 因此,它可以检测碰撞 如果我使用自动模拟,OnCollisionCenter可以工作。但是它不能 当我使用物理时工作。模拟它不注册 您不能使用C# 我们能用物理方法检测刚体碰撞的时间吗 public void运行模拟() { 物理。自动模拟=错误; SimulatedBody=FindObjectSoftType()。选择(rb=>new SimulatedBody(rb)).ToArray(); striker.GetComponent().AddForce(新矢量3(200.0f、0.0f、200.0f)); 对于(int i=0;ibody.rigidbody.IsSleeping()) { 印刷品(一); 打破 } } Physics.autoSimulation=true; },c#,unity3d,game-physics,C#,Unity3d,Game Physics,我试着用物理来统一模拟。根据unity文档,“模拟包括碰撞检测、刚体和关节集成以及物理回调(接触、触发和关节)归档的所有阶段。” 我需要知道我的striker游戏对象与其他游戏对象在哪个迭代中发生碰撞。如果我使用自动模拟,OnCollisionCenter可以工作。但是当我使用物理时它不工作。模拟它不注册。我做错了什么,或者当我的对象与其他对象碰撞时我如何获得 我们能用物理方法检测刚体碰撞的时间吗 统一 是的,它可以 我曾经对此做过一个实验,它成功了,我打开了使用的同一个示例场景,再次进行了测试
IsSleeping()
以获得碰撞。假设已正确设置碰撞器和刚体,则问题在于maxIterations
值太小,导致模拟未完成,且未到达预期与另一对象碰撞的目标。如果未完成此碰撞,OnCollisionEnter
将不会被调用
增加你的maxIterations
在模拟中给它更多的距离移动,应该调用OnCollisionCenter
。至于检测你的游戏对象与其他游戏对象碰撞的迭代,使用布尔变量作为标志,并在OnCollisionCenter
函数中将其设置为true
,然后当此布尔变量为true
时,将从for
循环中断开
public void RunSimulation()
{
Physics.autoSimulation = false;
simulatedBodies = FindObjectsOfType<Rigidbody>().Select(rb => new SimulatedBody(rb)).ToArray();
striker.GetComponent<Rigidbody> ().AddForce (new Vector3 (200.0f, 0.0f, 200.0f));
for (int i = 0; i < maxIterations; i++)
{
Physics.Simulate(0.02f);
//GET COLLISIONS HERE
if (simulatedBodies.All(body => body.rigidbody.IsSleeping()))
{
print(i);
break;
}
}
Physics.autoSimulation = true;
}
你说得对,它成功了。这是unity的一个非常好的特性。
const int maxIterations = 60;
int collideIteration = 0;
bool collided = false;
public void RunSimulation()
{
//RESET OLD VALUES
collided = false;
collideIteration = 0;
Physics.autoSimulation = false;
Rigidbody rbdy = GetComponent<Rigidbody>();
rbdy.AddForce(new Vector3(200.0f, 0.0f, 200.0f));
for (int i = 0; i < maxIterations; i++)
{
Physics.Simulate(0.02f);
//Check if collided then break out of the loop
if (collided)
{
collideIteration = i;
print(i);
break;
}
}
Physics.autoSimulation = true;
}
public int GetCollideIteration()
{
return collideIteration;
}
void OnCollisionEnter(Collision collision)
{
collided = true;
Debug.Log("Collision: " + collision.collider.name);
}
public void RunSimulation()
{
//RESET OLD VALUES
collided = false;
collideIteration = 0;
Physics.autoSimulation = false;
Rigidbody rbdy = GetComponent<Rigidbody>();
rbdy.AddForce(new Vector3(200.0f, 0.0f, 200.0f));
//Simulate where it will be in 5 seconds
float timeInSec = 5f;
int i = 0;
while (timeInSec >= Time.fixedDeltaTime)
{
timeInSec -= Time.fixedDeltaTime;
Physics.Simulate(Time.fixedDeltaTime);
//Check if collided then break out of the loop
if (collided)
{
collideIteration = i;
print(i);
break;
}
i++;
}
Physics.autoSimulation = true;
}