我真的需要一些关于我的游戏对象的帮助。
我正在制作一个游戏,想让拾取物品创建一个物理力爆炸来吹走敌人。我制作了一个简单的炸弹对象来测试这个想法。我添加了一个简单的代码,使用循环来收集其半径内的所有碰撞体,然后对这些碰撞体添加力。现在代码正常工作,但并不是所有的游戏对象都能正确地反应。
我查看了原因,并注意到当我给对象添加RigidBody组件时,我的Collider会从GameObject上掉落。RigidBody是需要AddForce影响的。当我移除rb时,collider保持在原地,但对象不会对物理力做出反应。
我添加了一些图片来说明我的意思:
我已经尝试过:
- 将所有组件设置从反应的立方体和石头Gameobjects复制/粘贴到叶子Gameobject中,同时禁用其他所有代码,例如C#脚本。碰撞体和RB不会穿过地板,但当物理力量击中碰撞体时,它被吹走而GameObject保持其位置。
- 尝试不同的GameObject /Collider类型。
- 删除水。
- 调整炸弹的力量/半径。
- 编辑GameObject的“标签”和“层样式”。
“Explosion Code”如下,但我认为问题不在代码中。我将代码添加到场景的主摄像机中,并将一个简单的球体作为“炸弹”GameObject。
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
}
}
}
}
如何让叶子的Rigidbody、Collider和GameObject像标准的3D对象“cube”一样彼此保持连接,这样我就可以像其他模型一样用物理力量将它们吹走。
感谢您抽出时间。我已经尝试了很多方法并在互联网上搜索了几个小时,但似乎找不到解决方案。