Hi,
I have written code of drawing line using line renderer by mouse drag, path is drawn, and then i want to follow mycube as per it with smoothly, my cube can follow for the first drag, if i draw, one more time, then it is not able to follow smoothly,
for that i have written below code , please suggest me what i am doing wrong?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Vectrosity;
public class DrawLine : MonoBehaviour
{
VectorLine myLine1;
VectorLine recentLine;
public Material lineMat;
private float startTime;
private float journeyLength;
public float speed = 1.0F;
public Vector3 lastPos;
Vector3[] myRoute;
private LineRenderer line;
private bool isMousePressed;
private List<Vector3> pointsList;
private Vector3 mousePos;
private bool startLineAnimation;
// Structure for line points
struct myLine
{
public Vector3 StartPoint;
public Vector3 EndPoint;
};
// -----------------------------------
void Awake()
{
line = gameObject.GetComponent<LineRenderer> ();
line.SetVertexCount(0);
line.material = lineMat;
line.renderer.enabled = true;;
line.useWorldSpace = true;
isMousePressed = false;
startLineAnimation = false;
pointsList = new List<Vector3>();
}
// -----------------------------------
void Update ()
{
// If mouse button down, remove old line and set its color to green
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.transform.gameObject == transform.gameObject)
{
isMousePressed = true;
line.SetVertexCount(0);
pointsList.RemoveRange(0,pointsList.Count);
}
}
//line.SetColors(Color.green, Color.green);
}
else if(Input.GetMouseButtonUp(0))
{
if(isMousePressed)
{
isMousePressed = false;
DrawMe();
}
}
// Drawing line when mouse is moving(presses)
if(isMousePressed)
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z=0;
if (!pointsList.Contains (mousePos))
{
pointsList.Add (mousePos);
line.SetVertexCount (pointsList.Count);
line.SetPosition (pointsList.Count-1 , (Vector3)pointsList [pointsList.Count-1]);
// StartCoroutine(add (mousePos));
// if(isLineCollide())
// {
// isMousePressed = false;
// //line.SetColors(Color.red, Color.red);
// }
}
}
if (startLineAnimation && recentLine.active) {
recentLine.textureOffset = -Time.time * 2.0f % 1f;
StartCoroutine(StartFollowing());
}
}
IEnumerator StartFollowing () {
int counter = 0;
Vector3 pointA = transform.position;
Vector3 pointB = myRoute[counter];
print ("pointA:"+pointA);
print ("pointB:"+pointB);
while (counter<=myRoute.Length-1) {
yield return StartCoroutine(MoveObject(transform, pointA, pointB, 3.0f));
counter++;
if(counter<=myRoute.Length-1)
{
pointA=pointB;
pointB = myRoute[counter];
}
//yield return StartCoroutine(MoveObject(transform, pointB, pointA, 3.0f));
}
}
IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time) {
var i= 0.0f;
var rate= 2.0f/time;
while (i < 1.0f) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
}
public void DrawMe()
{
line.enabled = false;
myRoute = new Vector3[pointsList.Count];
int i ;
for (i=0; i<pointsList.Count; i++)
{
myRoute _= pointsList*;*_
_ print(i+" : "+myRoute*); *_
* }*
* lastPos = myRoute[i-1];*
* print (lastPos);*
* VectorLine.Destroy(ref myLine1);*
* myLine1 = new VectorLine (“Line1”, myRoute, lineMat, 2.5f, LineType.Continuous);*
* myLine1.textureScale = 5.0f;*
* recentLine = myLine1;*
* myLine1.Draw ();*
* startLineAnimation = true;*
* //for (int i=0; i<myRoute.Length; i++)*
* //{*
* startTime = Time.time;*
* journeyLength = Vector3.Distance(transform.position, lastPos); *
* //}*
_ /myLine1 = VectorLine.SetLine (Color.cyan, myRoute);
myLine1.smoothWidth = true;
myLine1.material = lineMat;/
* }*_
}