Unity中利用贝塞尔曲线来实现3D中的曲线运动

原理可以看下我转载的<贝塞尔曲线的数学原理>

下面是在理解后写的2阶三阶曲线

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

public class TestCurve : MonoBehaviour
{

    public float Timer = 0;
    public float Time1 = 2f;
    public List Points = new List();
    public List Game_Trans;
    public float MoveSpeed = 2f;
    public GameObject Ball;
    int i = 0;

    void Awake()
    {
        ThirdOrder();
    }

    void Update()
    {
        Timer += Time.deltaTime;
        if (Timer > Time1)
        {
            i++;
            if (i >= Points.Count - 1)
                i = 0;
            Timer = 0;
            Ball.transform.position = Vector3.Lerp(Points[i], Points[i + 1], 1);
           
        }
    }

    /// 
    /// 二阶贝塞尔曲线
    /// 
    public void SecondOrder()
    {
        Points = new List();
        for (int i = 0; i < 200; i++)//for循环计算200个点,
        {
            Vector3 v1 = Vector3.Lerp(Game_Trans[0].position, Game_Trans[1].position, i / 100f);//注意:这里差值运算时间t必须相同
            Vector3 v2 = Vector3.Lerp(Game_Trans[1].position, Game_Trans[2].position, i / 100f);

            var find = Vector3.Lerp(v1, v2, i / 100f);
            Points.Add(find);
        }
    }

    /// 
    /// 三阶贝塞尔曲线
    /// 
    public void ThirdOrder()
    {
        Points = new List();
        for (int i = 0; i < 200; i++)//for循环计算200个点,
        {
            Vector3 v1 = Vector3.Lerp(Game_Trans[0].position, Game_Trans[1].position, i / 100f);//注意:这里差值运算时间t必须相同
            Vector3 v2 = Vector3.Lerp(Game_Trans[1].position, Game_Trans[2].position, i / 100f);
            Vector3 v3 = Vector3.Lerp(Game_Trans[2].position, Game_Trans[3].position, i / 100f);

            var v1_1 = Vector3.Lerp(v1, v2, i / 100f);
            var v1_2 = Vector3.Lerp(v2, v3, i / 100f);

            var find = Vector3.Lerp(v1_1, v1_2, i / 100f);
            Points.Add(find);
        }
    }
    //4阶5阶同理

    public void OnDrawGizmos()
    {
        ThirdOrder();

        Gizmos.color = Color.yellow;
        for (int i = 0; i < Points.Count - 1; i++)
        {
            Gizmos.DrawLine(Points[i], Points[i + 1]);
        }
    }
}

下面的代码是使用贝塞尔通用公式来实现的三阶曲线,
public class DrawLineTest : MonoBehaviour
{
	public Transform[] m_Trans;
	[SerializeField]
	public List m_Points;
	// Use this for initialization
	void Start()
	{
		BezierLine(200);
	}

	public void BezierLine(int _points)
	{
		m_Points = new List();
		Vector2 pointA = cubeBezier(m_Trans[0].position, m_Trans[1].position, m_Trans[2].position, m_Trans[3].position, 0f);
		m_Points.Add(pointA);
		for(int i = 0; i < _points; i++)
		{
			var vector = cubeBezier(m_Trans[0].position, m_Trans[1].position, m_Trans[2].position, m_Trans[3].position, i / (float)_points);
			m_Points.Add(vector);
		}
	}

	int i = 0;

	void OnDrawGizmos()
	{
		BezierLine(200);
		Gizmos.color = Color.yellow;
		while(i < m_Points.Count - 2)
		{
			Gizmos.DrawLine(m_Points[i], m_Points[i + 1]);
			i++;
		}
		i = 0;
		
	}

	private static Vector2 cubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
	{
		float num = 1f - t;
		return num * num * num * s + 3f * num * num * t * st + 3f * num * t * t * et + t * t * t * e;
	}
}




你可能感兴趣的:(Unity3D,数学,应用)