我将此脚本添加到敌方飞船,并尝试在玩家飞船上使用它,但弹丸仍然无法射击并杀死玩家飞船。我试图在敌舰上发射激光枪以杀死玩家飞船。但是我的脚本不起作用。在Unity的检查器中,我需要将玩家飞船添加到公共刚体弹丸中;因此我在尝试将脚本添加到敌方舰船或玩家舰船时做到了,但敌方的激光子弹仍然无法杀死玩家舰船。但是,敌人的飞船及其子弹会跟随玩家的飞船,尽管这样做是可行的。欢迎任何见识:/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectiles : MonoBehaviour
{
public Transform playerShip;
public float range = 20.0f;
public float enemyGunImpulse = 10.0f;
bool onRange = false;
public Rigidbody projectile;
void Start()
{
float rand = Random.Range(1.0f, 2.0f);
InvokeRepeating("Shoot", 2, rand);
}
void Shoot()
{
if (onRange)
{
Rigidbody enemyGun = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
enemyGun.AddForce(transform.forward * enemyGunImpulse, ForceMode.Impulse);
Destroy(enemyGun.gameObject, 2);
}
}
void Update()
{
onRange = Vector3.Distance(transform.position, playerShip.position) < range;
if (onRange)
transform.LookAt(playerShip);
}
}