- Home /
Question by
Darkskxcher · Jan 22, 2015 at 12:40 PM ·
c#linerendererwaypoint
Draw lines between waypoints (using lineRenderer at the moment)
Hello guys, I am trying to generate lines between waypoints. The thing I am trying to achieve is to draw a line from waypoint 1 to 2 and when your reach waypoint 2 it removes the lines between 1 and 2 and creates a new line between 2 and 3 and so on.
This is the code I have for my waypoints at the moment but I just can't seem to incorporate a linerenderer without getting lots of errors.
using UnityEngine;
using System.Collections;
public class Waypoints : MonoBehaviour
{
private Transform startMarker, endMarker;
public Transform[] waypoint;
private float speed = 1.0F;
private float startTime;
private float journeyLength;
// public Transform target;
private int currentStartPoint;
void Start ()
{
currentStartPoint = 0;
SetPoints ();
}
void SetPoints ()
{
startMarker = waypoint [currentStartPoint];
endMarker = waypoint [currentStartPoint + 1];
startTime = Time.time;
journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
}
void Update ()
{
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp (startMarker.position, endMarker.position, fracJourney);
if (fracJourney >= 1f && currentStartPoint + 1 < waypoint.Length) {
currentStartPoint++;
SetPoints ();
}
}
}
Comment
Best Answer
Answer by jenci1990 · Jan 22, 2015 at 07:10 PM
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(LineRenderer))]
public class Waypoints : MonoBehaviour {
private Transform startMarker, endMarker;
public Transform[] waypoint;
private float speed = 2f;
private float startTime = 0f;
private float journeyLength = 1f;
private int currentStartPoint = 0;
private LineRenderer lineRenderer;
void Start() {
currentStartPoint = 0;
SetPoints();
lineRenderer = GetComponent<LineRenderer>();
}
void SetPoints() {
startMarker = waypoint[currentStartPoint];
endMarker = waypoint[currentStartPoint + 1];
startTime = Time.time;
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
}
void Update() {
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
Vector3 startPosition = startMarker.position;
Vector3 endPosition = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
lineRenderer.SetPosition(0, startPosition);
lineRenderer.SetPosition(1, endPosition);
if (fracJourney >= 1f) {
if (currentStartPoint + 2 < waypoint.Length) {
currentStartPoint++;
SetPoints();
} else {
//if finished, disable lineRenderer and this script
lineRenderer.enabled = false;
this.enabled = false;
}
}
}
}
Thank you so much, this was exactly what I needed! I tweaked it around and now it works perfect.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Draw shortest distance between 2 points on a sphere 1 Answer
Line Renderer Issues 1 Answer
Need an advise to upgrade my code!!! 1 Answer