- Home /
How can I use linerenderer with world space and to be able to move the objects ?
I'm using linerenderer to draw triangles by looping over vertices. If I'm using world space it will draw the triangles fine but then I cant change the linerenderer object positions:
This is a screenshot when using world space with the linerenderer. It seems to me that it's drawing all the triangles no sure if all the triangles completed but it looks like they are. But I can't move now the lines:
Now I changed the script to not to use world space and the result is that all the triangles seems to be not completed each triangle have only two lines:
So I have two problems:
If world space is set to true it seems fine drawing but I can't move the lines. If world space is set to false I can move the lines but then the triangles are not completed.
This is my script:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Generator : MonoBehaviour
{
public GameObject meshPrefab;
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
private List<Vector3> verticesList = new List<Vector3>();
private List<Vector2> uvsList = new List<Vector2>();
private List<int> trianglesList = new List<int>();
private void Start()
{
Mesh meshprefab = meshPrefab.GetComponent<MeshFilter>().sharedMesh;
newVertices = meshprefab.vertices;
newTriangles = meshprefab.triangles;
for (int i = 0; i < newVertices.Length - 2; i++)
{
DrawLine(newVertices[i], newVertices[i + 1], Color.red);
DrawLine(newVertices[i + 1], newVertices[i + 2], Color.red);
DrawLine(newVertices[i + 2], newVertices[i], Color.red);
}
}
void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
{
GameObject myLine = new GameObject();
myLine.transform.position = start;
myLine.AddComponent<LineRenderer>();
LineRenderer lr = myLine.GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
lr.startColor = color;
lr.endColor = color;
lr.startWidth = 0.1f;
lr.endWidth = 0.1f;
lr.useWorldSpace = false;
lr.SetPosition(0, start);
lr.SetPosition(1, end);
}
}
Answer by zagoh · Sep 27, 2018 at 02:23 AM
@Chocolade Not sure if this help. But how I did mine was, when I am drawing all the lines, enable the line renderer's "use world space". However, when I want to move them, use a for-loop to change all the line renderer positions from world space to local space, then disable the line renderer's "use world space"
for(int i = 0; i
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i pick two randomly items from gameobject array ? 1 Answer
Script for a hue rainbow scroll on the material 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers