- Home /
Get Line Renderer to restart from start everytime using coroutine. HELP?!?!?!?
So I have a player that uses a line renderer animation to help with the trajectory.
The line render animates from one point to another using two game object positions.
The animation works perfectly the first time, however when re clicked the line renderer animation doesn't reset. It just stays at full length.
Any ideas on how to reset the position so the animation starts every time the mouse is clicked?
Any help would be greatly appreciated :)
LineRenderer link;
public Transform[] pos;
public float speed;
bool assist;
public void Update()
{
if (Input.GetMouseButton (0))
{
StartCoroutine(Assist());
assist = true;
}
if (Input.GetMouseButtonUp (0))
{
StartCoroutine(Assist());
assist = false;
}
}
public IEnumerator Assist()
{
link = gameObject.GetComponent<LineRenderer> ();
if (assist == true)
{
yield return null;
link.enabled = true;
link.SetPosition(0, pos[0].position);
link.SetPosition(1, Vector3.MoveTowards (transform.position,pos[1].position,speed *Time.time));
}
if (assist == false)
{
yield return null;
link.enabled = false;
}
}
}
Answer by Xquality · Nov 08, 2020 at 08:04 PM
@harrylongman11 Hope this helps. Consider using [SerializeField] instead of public if your intention is just to see and change the value in the inspector.
LineRenderer link;
public Transform[] pos;
public float speed = 2; //A speed of 2 is a good starting point
bool assist;
Vector3[] linkPos = new Vector3[2]; //Array containing two vector3
void Awake()
{
link = gameObject.GetComponent<LineRenderer>(); //We get components in Awake so that it only happens once
}
public void Update()
{
CheckInput(); //Try to keep your update functions neat by making them point to other functions
Assist();
}
void CheckInput()
{
assist = Input.GetMouseButton(0); //We assign the assist boolean to become our getmousebutton 0
}
void Assist()
{
link.enabled = assist;
if (link.enabled)
{
linkPos[0] = pos[0].position;
linkPos[1] = Vector3.Lerp(linkPos[1], pos[1].position, speed * Time.deltaTime);
//Using time.deltatime will make your line "animate" according to framerate, which is better in most cases
//Keep in mind that lerping a vector3 like this every frame might not be efficient, but it works
}
else
{
//Here we make the laser return home to the starting point, because you let go of the mouse button 0
linkPos[0] = pos[0].position;
linkPos[1] = pos[0].position;
}
link.SetPosition(0, linkPos[0]);
link.SetPosition(1, linkPos[1]);
}
Thank You!!!!!!!!
This worked great and your comments were extremely clear and helpful.
There is only one $$anonymous$$or issue. the two objects are in a straight line but when the player moves the second object (The direction of the line renderer) doesn't move to the updated position quick enough. So the line renderer never truly points forward. There's always a wobble.
Any ideas on fixing this?
But regardless thanks for the hellp :)))
Your answer
Follow this Question
Related Questions
Model stays at the end of my animation D:? 0 Answers
CORoutine does not completely stop 1 Answer
Animation Reset Issue 0 Answers
Get line Renderer code to restart animation every time mouse is clicked 1 Answer
iTween ValueTo onupdateparam help 1 Answer