为什么对撞机从游戏物体落下时添加一个刚体的?

问题描述 投票:3回答:1

我真的需要一些帮助,我GameObjects。

我工作的一种游戏,我想接机项目创建一个物理力爆炸炸毁了敌人。我做了一个简单的炸弹对象,以测试这个想法。我添加了一个简单的代码,使用循环收集半径内的所有对撞机,到随后addForce这些对撞机。现在代码工作正常,但不是所有的GameObjects正确反应。

我看着这是为什么,我发现我对撞机一直当我添加一个刚体组件对象从掉落游戏对象了。需要使用AddForce影响刚体。当我删除了RB,对撞机保持在适当位置,但对象不以物理力量做出反应。

我加了一些图片来说明我的意思:

Image of the Collider of the leaf sinking away..

Example image of objects which DO react to the AddForce.

我已经尝试过:

  • 而禁止所有其它代码如C#脚本复制/从反应立方石Gameobjects到叶游戏物体粘贴所有组件设置。对撞机&RB不属于通过地板但是当物理力打撞机吹走,而游戏对象保持其位置。 尝试不同的GameObjects /撞机类型。 除去水。 与炸弹力/半径的量播放。 编辑“标签”和游戏对象的“Layerstyle”。

在“爆炸代码”如下补充说,不过,我不认为这个问题是在代码中。我添加的代码到我的场景的主摄像头,并增加了一个简单的球为“炸弹”游戏对象。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Explosion : MonoBehaviour
{
    public GameObject bomb; //set the position of the explosion
    public float power = 10.0f;
    public float radius = 10.0f;
    public float upForce = 0.0f;

    private void FixedUpdate()
    {
        if (Input.GetKeyDown("space"))
        {
            print("space key was pressed");
            Invoke("Detonate", 1);
        }
    }

    void Detonate()
    {
        Vector3 explosionPosition = bomb.transform.position; //set the position of our explosion to the position of the bomb.
        Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius); //collects all colliders within the radius.
        foreach (Collider hit in colliders) { //for each individual collider the following code is ran.

            Rigidbody rb = hit.GetComponent<Rigidbody>(); //declare'rb' rigidbody. Get rb component from each collider
            if (rb != null)
            {
                print("BOOM!");
                rb.AddExplosionForce(power, explosionPosition, radius, upForce, ForceMode.Impulse); //add force to each collider
            }
        }

    }
}

如何使叶的刚体,对撞机和游戏对象守住对方喜欢标准的3D对象“魔方”,这样我就可以使这些吹散与物理力量就像其他车型?

谢谢你的时间,我一直在努力的事情,现在在互联网上四处寻找了几个小时,但似乎无法找到任何解决方案。

c# unity3d game-physics gameobject rigid-bodies
1个回答
0
投票

如果您在停止模式下,按播放添加刚体会发生什么?它搬开以类似的方式?这可能和预期的情况发生,如果你的对撞机相互交叉。只要你添加一个刚体,他们发现自己被困在严重的碰撞。

如果你不想修改场景,您可以在项目设置碰撞矩阵/物理拨弄

© www.soinside.com 2019 - 2024. All rights reserved.