如何制作仅命名为 GameObject 的 Raycast 效果

发布于 2024-11-09 00:37:17 字数 1094 浏览 0 评论 0原文

在我当前的(Unity 3.3 IOS)项目中,我有一个角色正在过桥。如果角色掉落 他要让桥掉进火河里爆炸。问题是,当他在桥上时,光线投射会读取桥上的刚体,然后他立即爆炸。如果我将他在场景窗口中重新定位到露天位置,重力会导致他落入河中并按计划爆炸。我添加了以下代码行来指定仅在光线投射击中“平面”时才销毁。这不起作用。

if(hit.collider.gameObject.name == "plane");

角色不会在桥上或撞到飞机/火河时被摧毁。

他将走进几座桥梁和建筑物,所以我只希望他在光线投射击中飞机时被摧毁/爆炸。

谁能告诉我为什么我的代码不起作用或者如何纠正它?

这是我完整的 Raycast 代码。

var explosion : Transform;
var point : Vector3;
var explosionRotation : Quaternion;

function Update()
{
    var hit :RaycastHit;
    var dwn = transform.TransformDirection(Vector3.down);
    if (Physics.Raycast(this.transform.position,dwn,hit,3))           
        if (hit.collider.gameObject.name == "plane")
        {
            point = hit.point;
            explosionRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
            Explode();
        }
}

function Explode()
{
    Destroy(this.gameObject);
    var instanExplosion = Instantiate(explosion, point, explosionRotation);
}

In my current(Unity 3.3 IOS)project I have a character walking across a bridge. If the character falls off
the bridge he is to fall into a fiery river and explode. The problem is when he's on the bridge the Raycast reads the rigidbody on the bridge and he immediately explodes. If I reposition him in the scene window to an open air position, the gravity causes him to fall into the river and explode as planned. I added the following line of code to designate only destroy if the raycast hits the "plane". It doesn't work.

if(hit.collider.gameObject.name == "plane");

The character does not get destroyed on the bridge or when he hits the plane/fiery river.

There are several bridges and buildings he will be walking into, so I only want him to be destroyed/explode if the raycast hits the plane.

Can anyone tell me why my code isn't working or how to correct it?

Here is my complete Raycast code.

var explosion : Transform;
var point : Vector3;
var explosionRotation : Quaternion;

function Update()
{
    var hit :RaycastHit;
    var dwn = transform.TransformDirection(Vector3.down);
    if (Physics.Raycast(this.transform.position,dwn,hit,3))           
        if (hit.collider.gameObject.name == "plane")
        {
            point = hit.point;
            explosionRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
            Explode();
        }
}

function Explode()
{
    Destroy(this.gameObject);
    var instanExplosion = Instantiate(explosion, point, explosionRotation);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2024-11-16 00:37:17

您能分享一下您的场景以及所涉及的基本对象吗?

可能会出现一些问题。

  • 桥也可能被命名为“平面”
  • 光线太长

你考虑过使用图层吗?听起来你使用那条射线只是为了角色的坠落/死亡目的。您可能希望将平面添加到单独的图层中,并让光线仅检查该图层。

Can you share your scene with the basic objects that are involved?

There are some problems that might occur.

  • The bridge might be named "plane", too
  • The ray is too long

Have you considered using layers? It sound like you use that ray only for falling/dying purpose for the character. You might want to add your plane to a separate layer and let the ray only check against that layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文