栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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().currentHealth -= 10;
                    hit.transform.GetComponent().clip = hit.transform.GetComponent().metalShooted;
                    hit.transform.GetComponent().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().currentBlood -= 10;
                    solider.GetComponent().nowState = 4;
                    solider.GetComponent().SetInteger("soliderState", 4);
                    solider.GetComponent().SetInteger("damageState", (int)Random.Range(1f, 3.99f));
                    solider.GetComponent().waitTime = 0.667f;
                    solider.GetComponent().nevrousTime = 10f;
                    solider.GetComponent().clip = solider.GetComponent().bodyShooted;
                    solider.GetComponent().Play();
                }
                if (hit.transform.tag == "SoliderHead")
                {
                    Transform solider = hit.transform.parent;
                    solider.GetComponent().clip = solider.GetComponent().headShooted;
                    solider.GetComponent().Play();
                    solider.GetComponent().currentBlood -= 110;
                }
                Destroy(gameObject);
            }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号