最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity使用DoTween實(shí)現(xiàn)拋物線效果

 更新時(shí)間:2021年05月27日 09:00:35   作者:輕風(fēng)點(diǎn)語(yǔ)  
這篇文章主要為大家詳細(xì)介紹了Unity使用DoTween實(shí)現(xiàn)拋物線效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Unity使用DoTween實(shí)現(xiàn)拋物線效果,供大家參考,具體內(nèi)容如下

概要

public partial class EMath
{
    public static Vector3 Parabola(Vector3 start, Vector3 end, float height, float t)
    {
        float Func(float x) => 4 * (-height * x * x + height * x);

        var mid = Vector3.Lerp(start, end, t);

        return new Vector3(mid.x, Func(t) + Mathf.Lerp(start.y, end.y, t), mid.z);
    }

    public static Vector2 Parabola(Vector2 start, Vector2 end, float height, float t)
    {
        float Func(float x) => 4 * (-height * x * x + height * x);

        var mid = Vector2.Lerp(start, end, t);

        return new Vector2(mid.x, Func(t) + Mathf.Lerp(start.y, end.y, t));
    }
}

使用方法

public class Test : MonoBehaviour
{
    public Transform start;
    public Transform target;
    public Transform ball;

    private float t;

    private void Start()
    {
        DOTween.To(setter: value =>
            {
                Debug.Log(value);
                ball.position = Parabola(start.position, target.position, 10, value);
            }, startValue: 0, endValue: 1, duration: 5)
            .SetEase(Ease.Linear);
    }
}

效果演示

之前小編收藏了一段拋物線代碼,分享給大家:unity實(shí)現(xiàn)炮彈運(yùn)動(dòng)軌跡(拋物線)

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

public class Parabol : MonoBehaviour
{
    private Rigidbody rgb;
    /// <summary>
    /// 目標(biāo)
    /// </summary>
    public GameObject target;
    /// <summary>
    /// 子彈的發(fā)射點(diǎn)
    /// </summary>
    private Vector3 originPoint;

    private Vector3 aimPoint;
    /// <summary>
    /// 無(wú)彈道偏移的當(dāng)前位置
    /// </summary>
    private Vector3 myVirtualPosition;
    /// <summary>
    /// 定位最后一幀
    /// </summary>
    private Vector3 myPreviousPosition;
    /// <summary>
    /// 是否可以發(fā)射
    /// </summary>
    private bool sw = false;
    private bool actived = false;
    /// <summary>
    /// 最大發(fā)射距離
    /// </summary>
    public float maxLaunch = 1f;
    /// <summary>
    /// 加速度計(jì)算計(jì)數(shù)器
    /// </summary>
    private float counter;
    /// <summary>
    /// 剛剛啟動(dòng)時(shí)的速度
    /// </summary>
    public float speed = 0.5f;
    /// <summary>
    /// 恒定加速度
    /// </summary>
    public float speedUpOverTime = 0.1f;
    /// <summary>
    /// 彈道偏移量(與目標(biāo)的距離)
    /// </summary>
    public float ballisticOffset = 0.5f;

    void Start()
    {
        rgb = GetComponent<Rigidbody>();
        sw = true;
        if (target == null)
        {
            Destroy(gameObject);
        }
        else
        {
            aimPoint = target.transform.position;
        }
        originPoint = myVirtualPosition = myPreviousPosition = transform.position;
        
    }
    
    void Update()
    {
        if (target != null)
        {
            if (actived == false)
            {
                actived = true;
                PreLaunch();
            }
            else
            {
                if (sw == true)
                {
                    if (rgb.isKinematic == false)
                    {
                        Move();
                    }
                }
            }
        }
    }
    private void PreLaunch()
    {
        float xTarget = target.transform.position.x;
        float yTarget = target.transform.position.y;
        float zTarget = target.transform.position.z;
        float xCurrent = transform.position.x;
        float yCurrent = transform.position.y;
        float zCurrent = transform.position.z;
        //目標(biāo)之間的值
        float xDistance = Mathf.Abs(xTarget - xCurrent);
        float yDistance = yTarget - yCurrent;
        float zDistance = Mathf.Abs(zTarget - zCurrent);
        float fireAngle = 1.57075f - (Mathf.Atan((Mathf.Pow(maxLaunch, 2f) + Mathf.Sqrt(Mathf.Pow(maxLaunch, 4f) - 9.8f * (9.8f * Mathf.Pow(xDistance, 2f) + 2f * yDistance * Mathf.Pow(maxLaunch, 2f)+ 2f * zDistance * Mathf.Pow(maxLaunch, 2f)))) / (9.8f * xDistance)));
        float xSpeed = Mathf.Sin(fireAngle) * maxLaunch;
        float ySpeed = Mathf.Cos(fireAngle) * maxLaunch;
        float zSpeed = Mathf.Tan(fireAngle) * maxLaunch;
        //判斷在左邊還是右邊
        if ((xTarget - xCurrent) < 0f) { xSpeed = -xSpeed; }    
        if ((zTarget - zCurrent) < 0f) { zSpeed = -zSpeed; }
        Calculation(ySpeed);                                                
        sw = true;
    }
    private void Calculation(float speedy)
    {
        NextPosition(Time.time % ((speedy / 9.81f) * 2));
    }
    private void NextPosition(float airtime)
    {
        float xTarget = target.transform.position.x;
        float yTarget = target.transform.position.y;
        float zTarget = target.transform.position.z;
        float speedy = target.GetComponent<Rigidbody>().velocity.y;
        float speedx = target.GetComponent<Rigidbody>().velocity.x;
        float speedz = target.GetComponent<Rigidbody>().velocity.z;
        Launch(xTarget + (speedx * airtime), yTarget + (speedy * airtime),zTarget+ (speedz * airtime));
    }
    private void Launch(float xTarget, float yTarget, float zTarget)
    {
        rgb.isKinematic = false;
        float xCurrent = transform.position.x;
        float yCurrent = transform.position.y;
        float zCurrent = transform.position.z;
        float xDistance = Mathf.Abs(xTarget - xCurrent);
        float yDistance = yTarget - yCurrent;
        float zDistance = Mathf.Abs(zTarget - zCurrent); 
        float fireAngle = 1.57075f - (Mathf.Atan((Mathf.Pow(maxLaunch, 2f) + Mathf.Sqrt(Mathf.Pow(maxLaunch, 4f) - 9.8f * (9.8f * Mathf.Pow(xDistance, 2f) + 2f * yDistance * Mathf.Pow(maxLaunch, 2f) + 2f * zDistance * Mathf.Pow(maxLaunch, 2f)))) / (9.8f * xDistance)));
        float xSpeed = Mathf.Sin(fireAngle) * maxLaunch;
        float ySpeed = Mathf.Cos(fireAngle) * maxLaunch;
        float zSpeed = Mathf.Tan(fireAngle) * maxLaunch;
        //判斷在左邊還是右邊
        if ((xTarget - xCurrent) < 0f) { xSpeed = -xSpeed; } 
        if (!float.IsNaN(xSpeed) && !float.IsNaN(ySpeed))
        {
            rgb.velocity = new Vector3(xSpeed, ySpeed, zSpeed);
        }
        else
        {
            maxLaunch = maxLaunch + 0.3f;
            PreLaunch();
        }
    }

    private void Move()
    {
        counter += Time.fixedDeltaTime;
        //加速度提升
        speed += Time.fixedDeltaTime * speedUpOverTime;
        if (target != null)
        {
            aimPoint = target.transform.position;
        }
        //計(jì)算從發(fā)射點(diǎn)到目標(biāo)的距離
        Vector3 originDistance = aimPoint - originPoint;
        //計(jì)算剩余距離
        Vector3 distanceToAim = aimPoint - myVirtualPosition; //發(fā)射點(diǎn)和目標(biāo)之間的矢量距離
        //移動(dòng)到目標(biāo)
        myVirtualPosition = Vector3.Lerp(originPoint, aimPoint, counter * speed / originDistance.magnitude);// vector nội suy giữa vị trí ban đầu và mục tiêu
        //向軌跡添加彈道偏移
        transform.position = AddBallisticOffset(originDistance.magnitude, distanceToAim.magnitude);
        //將子彈旋轉(zhuǎn)至彈道
        //Debug.Log("最后一幀的位置:" + myPreviousPosition);
        LookAtDirection(transform.position - myPreviousPosition);
        myPreviousPosition = transform.position;
    }
    private Vector3 AddBallisticOffset(float originDistance, float distanceToAim)
    {
        if (ballisticOffset > 0f)
        {
            // 計(jì)算彎曲處偏移
            float offset = Mathf.Sin(Mathf.PI * ((originDistance - distanceToAim) / originDistance));
            offset *= originDistance;
            // 向軌跡添加偏移
            return myVirtualPosition + (ballisticOffset * offset * Vector3.up);
        }
        else
        {
            return myVirtualPosition;
        }
    }

    /// <summary>
    /// 朝向目標(biāo)
    /// </summary>
    /// <param name="direction"></param>
    private void LookAtDirection(Vector3 direction)
    {
        Quaternion netPointQ = Quaternion.FromToRotation(direction, direction-transform.position);
        transform.rotation = Quaternion.Lerp(transform.rotation, netPointQ, 30f);
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity實(shí)現(xiàn)音頻播放管理器

    Unity實(shí)現(xiàn)音頻播放管理器

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)音頻播放管理器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng)

    Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)單機(jī)游戲每日簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C#中圖片的Base64編碼與解碼轉(zhuǎn)換詳解

    C#中圖片的Base64編碼與解碼轉(zhuǎn)換詳解

    在C#中,我們可以使用Base64編碼將圖片轉(zhuǎn)換為字符串,也可以將Base64編碼的字符串轉(zhuǎn)換回圖片,這通常用于在需要文本表示圖像數(shù)據(jù)的場(chǎng)合(例如在Web開(kāi)發(fā)中傳輸圖像數(shù)據(jù)),本文介紹了C#中圖片的Base64編碼與解碼轉(zhuǎn)換,需要的朋友可以參考下
    2024-12-12
  • C# 使用SDL2實(shí)現(xiàn)Mp4文件播放音視頻操作

    C# 使用SDL2實(shí)現(xiàn)Mp4文件播放音視頻操作

    這篇文章主要介紹了C# 使用SDL2實(shí)現(xiàn)Mp4文件播放音視頻操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • C#沉淀之委托的深入講解

    C#沉淀之委托的深入講解

    如果要給方法傳遞一個(gè)方法參數(shù)時(shí),就可以使用委托。下面這篇文章主要給大家介紹了關(guān)于C#沉淀之委托的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-09-09
  • C#裝箱和拆箱的原理介紹

    C#裝箱和拆箱的原理介紹

    這篇文章介紹了C#裝箱和拆箱的原理,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • 如何在C#中調(diào)用COM組件

    如何在C#中調(diào)用COM組件

    這篇文章主要介紹了如何在C#中調(diào)用COM組件,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#中如何利用正則表達(dá)式判斷字符

    C#中如何利用正則表達(dá)式判斷字符

    這篇文章主要介紹了C#中利用正則表達(dá)式判斷字符的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • Unity3D仿寫(xiě)B(tài)utton面板事件綁定功能

    Unity3D仿寫(xiě)B(tài)utton面板事件綁定功能

    這篇文章主要為大家詳細(xì)介紹了Unity3D仿寫(xiě)B(tài)utton面板事件綁定功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C#控制臺(tái)模擬電梯工作原理

    C#控制臺(tái)模擬電梯工作原理

    簡(jiǎn)單的模擬一下電梯的運(yùn)行,電梯內(nèi)部和外部樓層呼叫的優(yōu)先級(jí)判斷。以前學(xué)硬件的時(shí)候做這個(gè)不成問(wèn)題,現(xiàn)在用軟件來(lái)模擬對(duì)我來(lái)說(shuō)比較難,要C#的圖形界面。求高手賜教。
    2015-06-06

最新評(píng)論

电白县| 孙吴县| 岐山县| 龙陵县| 昭觉县| 鲁山县| 镇赉县| 红安县| 长武县| 青冈县| 姜堰市| 来安县| 漯河市| 罗田县| 盐边县| 墨玉县| 外汇| 塘沽区| 沽源县| 新河县| 中阳县| 鹿泉市| 青铜峡市| 保康县| 永德县| 甘肃省| 陕西省| 武安市| 工布江达县| 瑞安市| 杭州市| 西宁市| 叙永县| 阜南县| 二连浩特市| 壶关县| 剑河县| 潜江市| 合作市| 拉孜县| 会同县|