gpt4 book ai didi

c# - Unity 3D射击和摧毁敌人不起作用

转载 作者:行者123 更新时间:2023-12-05 00:44:51 29 4
gpt4 key购买 nike

我有一个玩家使用武器射击和摧毁敌人。我有枪的代码:

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);
}
}
}

即使我的敌人被标记为“敌人”并且触发了盒子对撞机,它也不会消失。子弹预制件具有刚体和球体对撞机。请帮忙:)

最佳答案

如果你使用 Destroy(gameObject) 你正在摧毁子弹。

为了消灭敌人,你应该做一个

销毁(other.gameObject)

所以你会摧毁真正触发的对象,敌人

关于c# - Unity 3D射击和摧毁敌人不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65429907/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com