Unity3D导弹自动追踪算法
3D建模技术文章教学modeling 666 4
实名

通过了实名认证的内容创造者

发布于 2019-3-18 09:31:18

您需要 登录 才可以下载或查看,没有账号?注册

x
核心代码:



using UnityEngine;

[RequireComponent(typeof(Collider),typeof(AudioSource))]
public class Missile : MonoBehaviour
{
    [SerializeField, Tooltip("最大转弯速度")]
    private float MaximumRotationSpeed = 120.0f;

    [SerializeField, Tooltip("加速度")]
    private float AcceleratedVeocity = 12.8f;

    [SerializeField, Tooltip("最高速度")]
    private float MaximumVelocity = 30.0f;

    [SerializeField, Tooltip("生命周期")]
    private float MaximumLifeTime = 8.0f;

    [SerializeField, Tooltip("上升期时间")]
    private float AccelerationPeriod = 0.5f;

    [SerializeField, Tooltip("爆炸特效预制体")]
    private Explosion[] ExplosionPrefabs = null;

    [SerializeField, Tooltip("导弹渲染体组件")]
    private Renderer MissileRenderer = null;

    [SerializeField, Tooltip("尾焰及烟雾粒子特效")]
    private ParticleSystem[] MissileEffects = null;

    [HideInInspector]
    public Transform Target = null;        // 目标
    [HideInInspector]
    public float CurrentVelocity = 0.0f;   // 当前速度

    private AudioSource audioSource = null;   // 音效组件
    private float lifeTime = 0.0f;            // 生命期

    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.loop = true;
        if (!audioSource.isPlaying)
            audioSource.Play();
    }

    // 爆炸
    private void Explode()
    {
        // 之所以爆炸时不直接删除物体,而是先禁用一系列组件,
        // 是因为导弹产生的烟雾等效果不应该立即消失

        // 禁止所有碰撞器
        foreach( Collider col in GetComponents<Collider>())
        {
            col.enabled = false;
        }
        // 禁止所有粒子系统
        foreach( ParticleSystem ps in MissileEffects)
        {
            ps.Stop();
        }
        // 停止播放音效
        if (audioSource.isPlaying)
            audioSource.Stop();

        // 停止渲染,停止本脚本,随机实例化爆炸特效,删除本物体
        MissileRenderer.enabled = false;
        enabled = false;
        Instantiate(ExplosionPrefabs[Random.Range(0, ExplosionPrefabs.Length)], transform.position, Random.rotation);

        // 三秒后删除导弹物体,这时候烟雾已经散去,可以删掉物体了
        Destroy(gameObject, 3.0f);
    }

    private void Update()
    {
        float deltaTime = Time.deltaTime;
        lifeTime += deltaTime;

        // 如果超出生命周期,则直接爆炸。
        if( lifeTime > MaximumLifeTime )
        {
            Explode();
            return;
        }

        // 计算朝向目标的方向偏移量,如果处于上升期,则忽略目标
        Vector3 offset =
            ((lifeTime < AccelerationPeriod) && (Target != null))
            ? Vector3.up
            : (Target.position - transform.position).normalized;

        // 计算当前方向与目标方向的角度差
        float angle = Vector3.Angle(transform.forward, offset);

        // 根据最大旋转速度,计算转向目标共计需要的时间
        float needTime = angle / ( MaximumRotationSpeed * ( CurrentVelocity / MaximumVelocity ));

        // 如果角度很小,就直接对准目标
        if (needTime < 0.001f)
        {
            transform.forward = offset;
        }
        else
        {
            // 当前帧间隔时间除以需要的时间,获取本次应该旋转的比例。
            transform.forward = Vector3.Slerp(transform.forward, offset, deltaTime / needTime).normalized;
        }

        // 如果当前速度小于最高速度,则进行加速
        if (CurrentVelocity < MaximumVelocity )
            CurrentVelocity += deltaTime * AcceleratedVeocity;

        // 朝自己的前方位移
        transform.position += transform.forward * CurrentVelocity * deltaTime;
    }

    private void OnTriggerEnter(Collider other)
    {
        // 当发生碰撞,爆炸
        Explode();
    }
}

---------------------
作者:sdhexu
来源:CSDN
原文:https://blog.csdn.net/sdhexu/article/details/77962134
版权声明:本文为博主原创文章,转载请附上博文链接! image.png


20170913154304531.gif

评分

参与人数 3元素币 +21 活跃度 +53 展开 理由
大西几 + 5 + 15 谢谢分享
元素大使瓜... + 18 【给力】阅贴无数,楼主最强!
首席游骑兵... + 16 + 20 【给力】阅贴无数,楼主最强!

查看全部评分

生命在于。。。永不停止的学习!
使用道具 <
想学插画的产品  发表于 2019-3-18 09:34:30  
2#
当初做毕设的时候用的代码,一年后再看真的头疼
回复 收起回复
使用道具
qq_马卡龙_WRh  发表于 2019-3-18 09:36:23  
3#
有点硬核
回复 收起回复
使用道具
㊣面包℡/zhh  发表于 2019-3-18 10:02:11  
4#
回复 收起回复
使用道具
颐韶  发表于 2019-4-21 17:36:12  
5#

资源哪里好,肯定元素找!
回复 收起回复
使用道具
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表