unity射线检测高速飞行的子弹是否打到敌人

该代码示例展示了如何在Unity中利用射线检测判断子弹是否击中敌人,根据命中部位(身体或头部)改变敌人状态并播放相应音效。SoliderAmmoController类控制子弹运动,通过Physics.Raycast进行碰撞检测,根据hit.transform.tag区分击中类型,调整敌人的血量和动画状态。
摘要由CSDN通过智能技术生成

利用射线检测,检测两帧子弹位置连线是否碰撞敌人,并改变敌人状态。

控制敌人射出的子弹的类如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoliderAmmoController : MonoBehaviour
{
    public float velocity = 50f;
    public float dis;
    public Vector3 posRecord;
    public Ray ray;
    public RaycastHit hit;
    public int damage = 10;
    float flyTime = 0f;
    Vector3 gravity;

    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject, 2.5f);
        gravity = new Vector3(0f, 4.9f, 0f);
    }

    // Update is called once per frame
    void Update()
    {
        ammoMovement();
    }
    void ammoMovement()
    {
        flyTime += Time.deltaTime;
        posRecord = transform.position;
        transform.position += (transform.forward * velocity * Time.deltaTime-0*gravity*flyTime*flyTime);
        dis = (posRecord - transform.position).magnitude;
        if (dis > 0)
        {
            if (Physics.Raycast(posRecord, transform.forward, out hit,dis))
            {
                if (hit.transform.tag == "Player")
                {
                    hit.transform.GetComponent<CarController>().currentHealth -= 10;
                    hit.transform.GetComponent<AudioSource>().clip = hit.transform.GetComponent<CarController>().metalShooted;
                    hit.transform.GetComponent<AudioSource>().Play();
                }
                Destroy(gameObject);
            }
        }
    }
}

代码说明:

首先要声明射线类Ray和射线击中返回信息类RaycastHit的实例

    public Ray ray;
    public RaycastHit hit;

之后调用射线检测的函数,其中Physics.Raycast的输入数据类型为Physics.Raycast(Vector3,Vector3,RaycastHit,float),第一个是发射点,第二个是射线方向,第三个是碰撞物信息,第四个是射线长度,输出为bool型。

下面是检查玩家子弹是否打中敌人的程序逻辑,分为打中身体和爆头两种情况。

            if (Physics.Raycast(posRecord, transform.forward, out hit, dis))
            {
                if (hit.transform.tag == "SoliderBody")
                {
                    Transform solider = hit.transform.parent;
                    solider.GetComponent<SoliderController>().currentBlood -= 10;
                    solider.GetComponent<SoliderController>().nowState = 4;
                    solider.GetComponent<Animator>().SetInteger("soliderState", 4);
                    solider.GetComponent<Animator>().SetInteger("damageState", (int)Random.Range(1f, 3.99f));
                    solider.GetComponent<SoliderController>().waitTime = 0.667f;
                    solider.GetComponent<SoliderController>().nevrousTime = 10f;
                    solider.GetComponent<AudioSource>().clip = solider.GetComponent<SoliderController>().bodyShooted;
                    solider.GetComponent<AudioSource>().Play();
                }
                if (hit.transform.tag == "SoliderHead")
                {
                    Transform solider = hit.transform.parent;
                    solider.GetComponent<AudioSource>().clip = solider.GetComponent<SoliderController>().headShooted;
                    solider.GetComponent<AudioSource>().Play();
                    solider.GetComponent<SoliderController>().currentBlood -= 110;
                }
                Destroy(gameObject);
            }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值