jjb*_*tte 3 c# 3d unity-game-engine 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 = 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
以及项目符号的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
Destroy(gameObject);
}
}
}
Run Code Online (Sandbox Code Playgroud)
即使我的敌人被标记为“敌人”并且触发了盒子碰撞器,它也不会消失。子弹预制件具有刚体和球体碰撞器。请帮忙 :)
如果您使用 Destroy(gameObject),您就会销毁子弹。
为了消灭敌人,你应该做
Destroy(other.gameObject)
所以你会摧毁实际触发的物体,敌人
| 归档时间: |
|
| 查看次数: |
2603 次 |
| 最近记录: |