C# Unity 3D射击和摧毁敌人不起作用
我有一个使用武器射击和消灭敌人的玩家。我有枪的密码:C# Unity 3D射击和摧毁敌人不起作用,c#,unity3d,3d,game-physics,C#,Unity3d,3d,Game Physics,我有一个使用武器射击和消灭敌人的玩家。我有枪的密码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Gun : MonoBehaviour { float bulletSpeed = 60; public GameObject bullet; void Fire(){ GameObject tempBullet = Instantia
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour {
float bulletSpeed = 60;
public GameObject bullet;
void Fire(){
GameObject tempBullet = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>();
tempRigidBodyBullet.AddForce(tempRigidBodyBullet.transform.forward * bulletSpeed);
Destroy(tempBullet, 5f);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Fire();
}
}
}
即使我的敌人被贴上“敌人”的标签,并且触发了一个盒子对撞机,它也不会消失。子弹预制有刚体和球体碰撞器。请帮忙:你在告诉子弹自毁。你可能更想
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
// Destroy the thing tagged enemy, not youself
Destroy(other.gameObject);
// Could still destroy the bullet itself as well
Destroy (gameObject);
}
}
你是在告诉子弹自毁。你可能更想
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
// Destroy the thing tagged enemy, not youself
Destroy(other.gameObject);
// Could still destroy the bullet itself as well
Destroy (gameObject);
}
}
如果你使用DestroygameObject,你就是在销毁子弹 为了消灭敌人,你应该采取行动 销毁其他游戏对象
因此,您将销毁实际触发的对象,即敌人如果您使用DestroygameObject,则您正在销毁子弹 为了消灭敌人,你应该采取行动 销毁其他游戏对象
所以你会破坏实际触发的物体,敌人你在inspector中有对撞机的触发布尔检查吗?你在inspector中有对撞机的触发布尔检查吗?对撞机其他包含子弹击中右侧的对撞机数据吗?其他是一个标记为IsTrigger的对撞机,它进入连接到子弹的对撞机是对撞机其他包含对撞机数据子弹击中了,对吗?另一个是一个标记为IsTrigger的对撞机,当我写下答案@derHugo已经回答时,它进入了连接到bullet Yessory的对撞机。接受他的回答抱歉,当我在写答案时,@derHugo已经回答了。接受他的回答