- Home /
moving forward after bazier curve
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
}
}
Answer by LCStark · Oct 02, 2018 at 03:02 PM
Where do you get this error? Is it this line?
Vector3 standpos = positions[currentIndex] - cube.transform.position;
I don't see you initializing the
currentIndex
anywhere in your code. In your
Start
method set its value to 0.
No, i’m. Getting it here
Vector3.$$anonymous$$oveTowards(cube.transform.position,positions[currentIndex],_speed * Time.deltaTime);
Looks like the same problem - currentIndex
is out of range of the positions
array. You could put a Debug.Log
to check for its value before that line. You still might want to initialize it to 0, though.
well, it's not going out of range anymore but I is still freeze after the curve and doesn't continue with move forward...
Your answer
Follow this Question
Related Questions
how to use t variable in the bazier curve 2 Answers
witch approach for an car race curve 1 Answer
How to get lateral normals of a bezier curve 1 Answer
Why is my ID value getting stuck here? 1 Answer
Access curves presets from c# 1 Answer