- Home /
Draw line in runtime
Hello everyone, Iam working in project that require player point some points on ground (or input cordinates of points) to draw lines between points then press go and my gameobject will move along that path (lines still exist when gameObject moving). How can i do that. Thanks alot.
Answer by vexe · Sep 15, 2013 at 03:51 AM
For drawing lines, check out the line renderer, here's some tuts: tut1, tut2.
You should have your points already in an array or a list. Draw the lines you want between them, and have your object move through the points one by one, use a Lerping function to smooth your movement. Something like:
IEnumerator MoveTo (Transform objectToMove, Vector3 targetPosition, float timeToTake)
{
float t = 0;
Vector3 originalPosition = objectToMove.position;
while (t < 1)
{
// putting the increment of t before the lerp, will make it so that by the end of the loop our object would reach
// its target position with 100% because t is clampped between 0 to 1 in the lerp.
// but if we but the increment after it - in the last run - t would become something like 0.99942 or something
// we'd lerp to that - and then t would go beyond 1 and the loop would exit - and we'd end up with our object sitting at 0.99942
// of the road - not 1
t += Time.deltaTime / timeToTake;
objectToMove.position = Vector3.Lerp (originalPosition, targetPosition, t);
yield return null;
}
}
Here's a very quick and dirty implementation (attach this to the object you're moving):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToPoints : MonoBehaviour
{
// Inspector variables, pre-set by you
public List<Transform> locations;
public GameObject trail; // instead of drawing lines, I'm gonna instantiate a trail object along the way
public float timeToTake = 1f;
private Transform _transform;
private bool hasReachedTarget;
// cached the transform component
void Awake()
{
_transform = transform;
}
IEnumerator Start()
{
foreach(Transform t in locations)
{
StartCoroutine(MoveTo(_transform, t.position, timeToTake));
while (!hasReachedTarget)
yield return null;
}
}
IEnumerator MoveTo(Transform objectToMove, Vector3 targetPosition, float timeToTake)
{
float t = 0;
Vector3 originalPosition = objectToMove.position;
hasReachedTarget = false;
while (t < 1) {
t += Time.deltaTime / timeToTake;
objectToMove.position = Vector3.Lerp(originalPosition, targetPosition, t);
Instantiate(trail, _transform.position, Quaternion.identity);
yield return null;
}
hasReachedTarget = true;
}
}
Result:
Credits to the original MoveTo
function goes to @whydoidoit from UnityGems (Specifically the Coroutines++ tutorial)
Your answer
Follow this Question
Related Questions
How to add collider2D to LineRenderer? 0 Answers
Generating lines between procedurally instantiated gameobjects, within a certain radius 1 Answer
Drawing 2d line from a point on an object! 1 Answer
How to draw a line in unity3d 3 Answers
Gameobject is drawing a line render,when it's not supposed to ! 0 Answers