作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个玩家使用武器射击和摧毁敌人。我有枪的代码:
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/
我需要实现射击机制。我需要射弹的连续运动,但我无法对其进行编码。此时,当我按下空格键时,程序就会卡住。我们将不胜感激。 from tkinter import * # creates window w
我遇到了一个问题。如果有任何帮助,我将不胜感激。 我正在尝试从玩家位置射击到鼠标点击位置。代码没有给我任何错误,根据我的逻辑,它应该可以工作,但它没有 它创建了项目符号对象,仅此而已。 //Bulle
我正在用 Java 开发一款 Android 游戏,其中我将有一个 Sprite 跟随用户的手指,并且应该每秒发射一颗子弹。换句话说,我试图附加一个每秒向上移动的位图。位图从主要角色 Sprite 的
我需要在 JQuery 中使用 focusin 和 focusout 触发器。我将它与 Bootstrap Popovers 一起使用。 目前,由于我认为某些奇怪的原因,它不起作用。 这有什么问题吗?
我是一名优秀的程序员,十分优秀!