【发布时间】:2021-04-02 09:48:54
【问题描述】:
我有一个使用武器射击和摧毁敌人的玩家。我有枪的代码:
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();
}
}
}
以及项目符号代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Destroy(gameObject);
}
}
}
即使我的敌人被标记为“敌人”并且触发了盒子对撞机,它也不会消失。子弹预制件有刚体和球体对撞机。请帮忙:)
【问题讨论】:
-
Inspector 中是否对碰撞器进行了触发布尔检查?
标签: c# unity3d 3d game-physics