Skip to content

Instantly share code, notes, and snippets.

@GuilleUCM
Created June 24, 2013 20:43
Show Gist options
  • Save GuilleUCM/5853454 to your computer and use it in GitHub Desktop.
Save GuilleUCM/5853454 to your computer and use it in GitHub Desktop.
Unity::Bezier curve degree 3
using UnityEngine;
using System.Collections;
public class BezierCurveDegree3 : MonoBehaviour {
public Vector3 m_startPoint;
public Vector3 m_endPoint;
public float m_time = 5.0f;
public float m_height = 1.0f;
private Vector3 m_controlPoint1;
private Vector3 m_controlPoint2;
private float m_bezierTime;
// Use this for initialization
void Start () {
m_bezierTime = 0.0f;
m_controlPoint1 = m_startPoint + new Vector3(0,m_height,0);
m_controlPoint2 = m_endPoint + new Vector3(0,m_height,0);
}
// Update is called once per frame
void Update () {
m_bezierTime+=Time.deltaTime;
float t = m_bezierTime/m_time;
if (t<1) {
float u = 1-t;
Vector3 p = m_startPoint*u*u*u; //first term
p += 3 * u * u * t * m_controlPoint1 ; //second term
p += 3 * u * t * t * m_controlPoint2 ; //second term
p += m_endPoint*t*t*t; //third term
transform.position = p;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment