【发布时间】:2021-05-13 07:34:51
【问题描述】:
我想让子弹跟随我的光线投射,用于检测子弹是否击中敌人。这是我目前拍摄光线投射的脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunShootingScript : MonoBehaviour
{
public ParticleSystem MuzzleFlash;
public float damage = 10f;
public float range = 100f;
public GameObject bullet;
public Camera playerCamera;
private void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Shoot();
}
}
void Shoot()
{
MuzzleFlash.Play();
RaycastHit hit;
if(Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, range))
{
TakeDamage target = hit.transform.GetComponent<TakeDamage>();
if(target != null)
{
target.GiveDamage(damage);
}
}
}
}
这是我让敌人受到伤害的脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TakeDamage : MonoBehaviour
{
public float health = 50f;
public void GiveDamage(float amount)
{
health -= amount;
if (health <= 0f)
{
Die();
}
}
public void Die ()
{
Destroy(gameObject);
}
// Start is called before the first frame update
}
我想让子弹穿过模拟子弹被射出的光线投射线,我需要在代码中添加什么来做到这一点?
【问题讨论】: