通过贝塞尔曲线实现旋转子弹

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;

public class Rota : MonoBehaviour
{
    public GameObject player;
    public GameObject prefab;

    public GameObject other;
    private void Start()
    {
        BeiSaiErMove();
    }
    
    public void BeiSaiErMove()
    {
        List<Vector3> list = new List<Vector3>();
        //集合中第一个是其实位置,也是在即当前所在位置
        list.Add(player.transform.position);
        float ang = 2 * Mathf.PI / 10;
        for (int i = 0; i < 20; i++)
        {
            float x = Mathf.Sin(i * ang) * (float)i / 2f;
            float y = Mathf.Cos(i * ang) * (float)i / 2f;
            float z = player.transform.position.z * (1 - (float)i / 40f) + other.transform.position.z * (i) / 40f;
            list.Add(new Vector3(x, y, z));
        }
        for (int i = 0; i < 20; i++)
        {
            float x = Mathf.Sin(i * ang) * (float)(20 - i) / 2f;
            float y = Mathf.Cos(i * ang) * (float)(20 - i) / 2f;
            float z = player.transform.position.z * (1 - (float)(i + 20) / 40f) + other.transform.position.z * (i + 20) / 40f;
            list.Add(new Vector3(x, y, z));
        }

        list.Add(other.transform.position);
        GameObject ph0 = Instantiate(prefab);
        Destroy(ph0, 5f);
        DOTween.To((t) =>
        {
            ph0.transform.position = BesselCurve(list.ToArray(), t);
        }, 0, 1, 3).OnComplete(() =>
        {
            print("打完了");
        });

        
    }
    public Vector3 BesselCurve(Vector3[] pos, float t)
    {
        Vector3[] arr = new Vector3[pos.Length - 1];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = pos[i] * (1 - t) + pos[i + 1] * t;
            Debug.DrawLine(pos[i], pos[i + 1], Color.red, 10);
        }
        if (arr.Length == 1)
        {
            return arr[0];
        }
        else
        {
            return BesselCurve(arr, t);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值