Unity missile automatic tracking algorithm

I searched the Internet for a long time, but I couldn't find a satisfactory tracking algorithm. It was either 2D or not realistic. I had to hold it back for a day, and finally got it out. The rendering:

( This picture is the effect of the original version, please see the link below for the improved project )

video demo

Missile Tracking Algorithm Demonstration

Core code:

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();
    }
}

 

The latest version of:

Click here to view a video demo.

Click here to download the demo upgrade version (with missile various parameter adjustment UI).

Click here to download the project source code

Guess you like

Origin blog.csdn.net/sdhexu/article/details/77962134