hello.
actually I have this script, i’m able to turn in a bazier curve, but a cube can’t continue moving in the direction where it is facing…
someone can tell me what i’m doing wrong?
i’m also getting an error that said me “rrays is out of range”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bezier : MonoBehaviour
{
[SerializeField]
private GameObject cube;
//private LineRenderer lineRenderer;
[SerializeField]
private Transform point0,point1,point2;
public Vector3[] positions = new Vector3[50];
private int numOfPoints = 50;
private int currentIndex;
private Vector3 currentPos;
private float _speed = 5.0f;
private bool curvato;
// Use this for initialization
void Start ()
{
//lineRenderer.positionCount = numOfPoints;
}
// Update is called once per frame
void Update ()
{
//DrawLinearCurve();
DrawQuadraticBazierCurve();
if ( curvato)
moveToward();
}
private void DrawQuadraticBazierCurve()
{
for (int i = 1; i <= numOfPoints; i++)
{
float t = i / (float) numOfPoints;
positions[ i - 1] = CalculateQuadraticBezierPoint(t,point0.position,point1.position,point2.position);
}
Vector3 standpos = positions[currentIndex] - cube.transform.position;
cube.transform.rotation = Quaternion.LookRotation(standpos);
cube.transform.position = Vector3.MoveTowards(cube.transform.position,positions[currentIndex],_speed * Time.deltaTime);
if (cube.transform.position == positions[currentIndex])
{
currentIndex++;
Debug.Log(currentIndex);
if(currentIndex > numOfPoints - 1)
{
Debug.Log("done!");
curvato = true;
}
}
}
private void moveToward()
{
Debug.Log("entrato!");
cube.transform.position += transform.forward * _speed * Time.deltaTime;
}
/*private Vector3 CalculateLinearBezierPoint (float t, Vector3 p0, Vector3 p1)
{
return p0 + t * (p1 - p0);
// P = P0 + t ( P1 - P0 )
}*/
private Vector3 CalculateQuadraticBezierPoint (float t, Vector3 p0, Vector3 p1, Vector3 p2)
{
float u = 1 - t;
float tt = t * t;
float uu = u * u;
Vector3 p = uu * p0;
p += 2 * u * t * p1;
p += tt * p2;
return p;
// B(t) = (1-t)2P0 + 2(1-t)tP1 + t2P2
}
}