I’m using the Kinect to move around two game object cubes that are attached to the hand joints. I’m trying to make it so that when the hands (cube game objects) are overlapping I can create a line, and as long as the cubes are still overlapping extra points will be added to the line continously.
This should give a drawing effect where a line is created and follows the hands when they are overlapping.
I’ve tried using an OnTriggerStart where if the two cubes overlap I create a line renderer and then add points every time the two game objects enter the trigger but so far this only creates a point on a line renderer like this:
It’s Unity 5.4.0f3
using UnityEngine;
using System.Collections;
public class LineCollider : MonoBehaviour {
public Color c1 = Color.green;
public Color c2 = Color.green;
public int lengthOfLineRenderer = 10;
public Material mat;
public int currentIndex = 0;
void Start() {
LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = mat;
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.3f, 0.3f);
lineRenderer.SetVertexCount(lengthOfLineRenderer);
for (int i = 0; i < lengthOfLineRenderer; i++) {
lineRenderer.SetPosition(i, new Vector3(0, 0, 0));
}
}
private void OnTriggerEnter(Collider collision) {
if (collision.gameObject.name == "Cube") {
LineRenderer lineRenderer = GetComponent<LineRenderer>();
Vector3 midpoint = new Vector3((transform.position.x + collision.transform.position.x) / 2, (transform.position.y + collision.transform.position.y) / 2, (transform.position.z + collision.transform.position.z) / 2);
AddPoint(lineRenderer, midpoint);
}
}
private void AddPoint(LineRenderer lr, Vector3 pos) {
if(currentIndex >= lengthOfLineRenderer) {
for(int i = 0; i < lengthOfLineRenderer; i++) {
lr.SetPosition(i, Vector3.zero);
currentIndex = 0;
}
}
if(currentIndex < lengthOfLineRenderer) {
lr.SetPosition(currentIndex, pos);
currentIndex += 1;
}
}
}