Question by
LordNipple · Feb 12, 2017 at 07:21 PM ·
triggerlinerenderertriggerslinesdrawline
Trying to trigger a series of lines using LineRenderer using keypress
link textHello o/
Edit: Attached the script as .txt as the code snippet pastes as a block
I have 6 origin transforms and a single destination transform. I am drawing lines between each origin and the destination. Currently the lines all draw when the game starts. I need each of the lines to be triggered using F1-F6. Full script is below, currently using a new script for each line (that is fine for what I am trying to do)
Any help is appreciated, thanks :)
linerenderer.txt
(1.6 kB)
Comment
Answer by karadag · Feb 13, 2017 at 07:09 AM
You can not use input code in Start function. Because its only calls once time. Please check this code. You can change keycode in inspector menu.Duplicate this gameobject for other keycode button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLine : MonoBehaviour
{
private LineRenderer lineRenderer;
private float counter;
private float dist;
public Transform origin;
public Transform destination;
public float lineDrawSpeed = 6f;
public KeyCode keyCode;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
}
// Called every frame
void Update()
{
if (Input.GetKeyUp(keyCode))
{
lineRenderer.SetPosition(0, origin.position);
lineRenderer.SetWidth(0.2f, 0.2f);
// Find distance as float between 2 vector transform points
dist = Vector3.Distance(origin.position, destination.position);
}
if (counter < dist)
{
// Increment counter mod draw speed, fed into Lerp
counter += .1f / lineDrawSpeed;
// Linear interpolation over time, limited by desired distance, returning current line length as float
float x = Mathf.Lerp(0, dist, counter);
Vector3 pointA = origin.position;
Vector3 pointB = destination.position;
// Find the desired direction as vector unit, multiply by current distance of line travelled (x) + starting point
Vector3 pointOnLine = x * Vector3.Normalize(pointB - pointA) + pointA;
// Update current position of line
lineRenderer.SetPosition(1, pointOnLine);
}
}
}