本文介绍了如何防止碰撞体相互穿越?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将游戏对象保存在封闭空间内.当他们到达边缘时,会有一些短暂的推力,但随后他们会直接穿过墙壁.

I am having trouble keeping game objects inside of a contained space. When they reach the edge, there is some momentary push back but then they will go right through the wall.

我在播放器上使用了 Box Collider,在关卡的墙上使用了 Mesh Collider.我对移动由玩家控制的玩家角色(太空船)都有问题.还有射弹,它们是火,忘记匀速移动.

I am using a Box Collider on the player, and a Mesh Collider for the level's wall. I am having issues with both a Player Character (a space ship) that the movement is controlled by the player. And with projectiles, which are fire and forget moving at a constant speed.

这是我的播放器的移动代码.它在 FixedUpdate() 函数中运行.

This is my movement code for my player. It is being run in the FixedUpdate() function.

//Movement
    haxis = Input.GetAxis("Horizontal") * speed;
    vaxis = Input.GetAxis("Vertical") * speed;

    moveVector.x = haxis;
    moveVector.z = vaxis;

    if(moveVector.magnitude > 1)
    {
        moveVector.Normalize();
    }

    rigidbody.MovePosition(transform.position + moveVector * speed);

对于子弹,它们被赋予一个速度,引擎计算它们的电影.他们正在使用 Box Collider 并将其设置为触发器,因此他们没有物理.但我使用 OnTriggerEnter 来销毁它们.

With the bullets, they are given a velocity and the engine calculates their moviements. They are using Box Collider and it is set as a Trigger so they don't have physics. But I use OnTriggerEnter to destroy them.

//Projectiles without physics collisiions
function OnTriggerEnter (other : Collider) {
    Destroy(gameObject);
}

当击中网格对撞机壁时,一些但不是全部的子弹会被摧毁.玩家有时会击中它并停下来,但通常可以穿过它.如何让网格碰撞器的碰撞每次都起作用?

Some, but not all of the bullets will be destroyed when hitting the mesh collider wall. The player will sometimes hit it and stop, but can usually push through it. How can I make the collisions with the mesh collider work every time?

推荐答案

与快速移动的物体发生碰撞总是一个问题.确保检测到所有碰撞的一个好方法是使用 Raycasting 而不是依赖物理模拟.这对子弹或小物体很有效,但对大物体不会产生很好的效果.http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

Collision with fast-moving objects is always a problem. A good way to ensure that you detect all collision is to use Raycasting instead of relying on the physics simulation. This works well for bullets or small objects, but will not produce good results for large objects.http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

伪代码(我这里没有代码补全,记性不好):

Pseudo-codeish (I don't have code-completion here and a poor memory):

void FixedUpdate()
{
    Vector3 direction = new Vector3(transform.position - lastPosition);
    Ray ray = new Ray(lastPosition, direction);
    RaycastHit hit;
    if (Physics.Raycast(ray, hit, direction.magnitude))
    {
        // Do something if hit
    }

    this.lastPosition = transform.position;
}

这篇关于如何防止碰撞体相互穿越?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 22:30
查看更多