如何制作仅命名为 GameObject 的 Raycast 效果
在我当前的(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能分享一下您的场景以及所涉及的基本对象吗?
可能会出现一些问题。
你考虑过使用图层吗?听起来你使用那条射线只是为了角色的坠落/死亡目的。您可能希望将平面添加到单独的图层中,并让光线仅检查该图层。
Can you share your scene with the basic objects that are involved?
There are some problems that might occur.
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.