Collider for Line Renderer. My code isnt working properly. (Collider out of place) [Unsolved for now]
Good morning!
I am working on a 3D graph using Force Atlas. I'm interested in showing the shared data between nodes by clicking on the lines between them. In order to do that, I want to add a collider to each line. The lines are a single Line Renderer moving across the nodes in a zig zag. I have done some research and I used a function from another topic of this forum. The problem is, the colliders are completely out of position and I ignore why.
Im going to share the class which the lines and colliders are generated and the node Generator class in case something is not correct there either. Also a picture of the problem. You will see there is no Line inside the collider.
Thanks a lot!!
EDIT: The code actually works, the collider receives the same values as the line renderer segment. But for some reason, everything is out of place, even the node pivot. This might be a Parenting issue.
//Line and Collider Colliders class using System.Collections; using System.Collections.Generic; using UnityEngine;
public class easy3DLine : MonoBehaviour
{
//Prepare Line render
private LineRenderer link;
private float distance;
//Prepare particle system
//public float m_Drift = 0.01f;
private void Start()
{
int numNodes = GameObject.FindGameObjectsWithTag("Influent").Length;
GameObject[] points = new GameObject[numNodes];
points = GameObject.FindGameObjectsWithTag("Influent");
link = GetComponent<LineRenderer>();
//InitializeIfNeeded();
link.positionCount = points.Length;
for (int i = 0; i < numNodes; i++)
{
Vector3 aux = new Vector3();
aux = points[Random.Range(0, numNodes)].transform.position;
//Range would be amount of particles
//link.SetPosition(0,m_Particles[i].position);
link.SetPosition(i, aux);
AddColliderToLine(link, points[i].transform.position, aux);
//staticUtil.edgeIncrease();
}
//staticUtil.nodeSet(link.positionCount);
//Debug.Log(staticUtil.nodeCount);
//Debug.Log(staticUtil.edgeCount);
}
private void AddColliderToLine(LineRenderer line, Vector3 startPoint, Vector3 endPoint)
{
//create the collider for the line
BoxCollider lineCollider = new GameObject("LineCollider").AddComponent<BoxCollider>();
//set the collider as a child of your line
lineCollider.transform.parent = line.transform;
// get width of collider from line
float lineWidth = line.endWidth;
// get the length of the line using the Distance method
float lineLength = Vector3.Distance(startPoint, endPoint);
// size of collider is set where X is length of line, Y is width of line
//z will be how far the collider reaches to the sky
lineCollider.size = new Vector3(lineLength, lineWidth, 1f);
// get the midPoint
Vector3 midPoint = (startPoint + endPoint) / 2;
// move the created collider to the midPoint
lineCollider.transform.position = midPoint;
//heres the beef of the function, Mathf.Atan2 wants the slope, be careful however because it wants it in a weird form
//it will divide for you so just plug in your (y2-y1),(x2,x1)
float angle = Mathf.Atan2((endPoint.z - startPoint.z), (endPoint.x - startPoint.x));
// angle now holds our answer but it's in radians, we want degrees
// Mathf.Rad2Deg is just a constant equal to 57.2958 that we multiply by to change radians to degrees
angle *= Mathf.Rad2Deg;
//were interested in the inverse so multiply by -1
angle *= -1;
// now apply the rotation to the collider's transform, carful where you put the angle variable
// in 3d space you don't wan't to rotate on your y axis
lineCollider.transform.Rotate(0, angle, 0);
}
/*
void InitializeIfNeeded()
{
if (m_System == null)
m_System = GetComponent<ParticleSystem>();
if (m_Particles == null || m_Particles.Length < m_System.main.maxParticles)
m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
}*/
}
//Node Generator Class using System.Collections; using System.Collections.Generic; using UnityEngine;
public class node3DGenerator : MonoBehaviour {
//AttachLineRenderer addLine;
private Vector3 distance;
private Vector3 scale;
//public Vector3 rotatorAxis;
public GameObject node3DPrefab;
public GameObject community;
public GameObject parentComm;
public int amountOfComm;
public int maxNodes;
private int numAux;
float x;
float y;
float z;
private static Random.State seedGenerator;
void Awake () {
parentComm = new GameObject();
parentComm.name = "Parent Comm";
parentComm.transform.position = new Vector3(0,0,0);
for (int i = 0; i < amountOfComm; i++)
{
x = Random.Range(2000, -2000);
y = Random.Range(2000, -2000);
z = Random.Range(2000, -2000);
distance = new Vector3(x, y, z);
GameObject commZone = Instantiate(community) as GameObject;
// GameObject commZone = new GameObject();
commZone.name = "Community " + i;
commZone.transform.localPosition = distance;
commZone.transform.parent = parentComm.transform;
numAux = Random.Range(maxNodes, 1);
Color color= Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
for (int u = 0; u < numAux; u++)
{
x = Random.Range(1000, -1000);
y = Random.Range(1000, -1000);
z = Random.Range(1000, -1000);
distance = new Vector3(x, y, z);
x = Random.Range(50, 10);
scale = new Vector3(x, x, x);
GameObject nodeInstance = Instantiate(node3DPrefab) as GameObject;
nodeInstance.transform.parent = commZone.transform;
nodeInstance.transform.localScale = scale;
nodeInstance.name = "NodeInfluent" + u;
nodeInstance.tag = "Influent";
nodeInstance.transform.localPosition = commZone.transform.position + distance;
//LineRenderer lineRenderer = nodeInstance.AddComponent<LineRenderer>();
/* addLine.line = lineRenderer;
addLine.start = nodeInstance.transform;
addLine.target= GameObject.FindGameObjectWithTag("Influent").transform;*/
// Renderer rend = nodeInstance.gameObject.GetComponent<Renderer>();
// rend.material.color = Color.red;
/*nodeInstance.transform.parent = commZone.transform;
commZone.transform.rotation += rotatorAxis * 360 / amountOfComm;*/
if (u == numAux)
break;
}
if (i == amountOfComm)
break;
}
}
// Update is called once per frame
void Update () {
}
}
Answer by xxmariofer · Jan 23, 2019 at 10:00 AM
Hello, have you tested creating rather than multiple box colliders using one edge collider? i have used it with line renderers and works really well, and its much easier than the way you are doing it since you can assign the edge colliders the points dinamically at the same time and in a really similar way you are doing it with the line renderer. the only collides with 2d colliders depending of your project that may not be an issue.
I haven't tried but I dont think I can use that. I want to show the shared info between two Nodes. If I use one edge collider with the same behaviour as the Line Renderer, how I can I put different triggers to show the info of each segments? If thats possible, I'd like to see how to code that.
In any case, the different box colliders script works, the problem is, they are not where they are supposed to be.
Thank you!
Ok then will not work for you, can you debug.log the startpoint and endpoint just for making sure they are the desired positions?
Well this is a mess. The code actually works. The collider receives the same data as the line renderer, but in display, not only the collider is out of place but the node pivot as well. This might be a Parenting problem.
Hey i think ii found your problem, the linerender position is local to the player, so if you make the linerenderer go from vector3 (0,0,0) to the (1,0,0) it wont go from the center of the map but from the center of the bbox and since the box is not in the center it wont be the same.
change this line lineCollider.transform.position = midPoint; and use the localPosition ins$$anonymous$$d of position, also the pivon oif the parent will always change if you put one extra child in a different position (will always be in the middle of the parent and childs)
It almost worked. The colliders are now in the Line Renderer segment position but they dont have the same angle and length. They are way shorter than the Line segment.
Answer by prakyathd801 · Nov 09, 2020 at 08:00 AM
//Adding Collider to line Renderer` BoxCollider2D lineCol = gameObject.AddComponent (); lineCol.transform.position = line.transform.position; lineCol.size = new Vector2(5f, 5f);` Note: This is not solution to above issue, But it may help someone to add collider to their line renderer..
Your answer
![](https://koobas.hobune.stream/wayback/20220612190309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# 3D Collider Without Fixed Points or use Mesh Filter Instead? 0 Answers
How to set box collider to a gizmos cube size 0 Answers
How To how ?? 2 Answers
Collision with object not working. 2 Answers
Help with Trigger Colliders.. 0 Answers