I have added a box collider2D to test whether the collider is present.When I tested the collider is present,not on the gameobject but behind the gameobject .How to add a collider to line renderer and detect collisions.
I’m no expert in Line Render but I’m assuming you can’t add one for the reason is that the line render acts as a ray. It’s not defined as a solid object. Instead you can use a bool to test whether or not the line is being it. Then if it is do something. Or using the box collider you can do something after a collision.
Yeah, I think you are trying to go about it backwards. Instead of seeing if something interacts with a line, you should check to see if the line interacts with anything else. Use a raycast for this.
The LineRenderer is more than a straight line, so it’s not a ray nor easily representable by a ray, unless it’s actually straight. I definitely remember seeing someone use a LineRenderer as a “drawn ground” sort of thing, and then have a ball roll over it. So adding collisions should be possible.
I believe in the video i saw he added an EdgeCollider 2D, but i’m not sure what you would in 3D. Technically tho, the a line is always 2D, and it may only be the visuals you can add that make it appear to be 3D. Definitely not an expert here either, just thought i’d mention the above.
The short answer is “you can’t”, the longer answer is “you can’t, but you can make your own collider that kinda acts like one”. If the line in question is just one segment (2 points), then this is actually pretty easy. Do the following anytime your LineRenderer’s positions are changed:
Create a new GameObject, add a Capsule Collider, set the “direction” to match the Z axis (2).
Set this object’s position to (A + B) * 0.5, where A and B are your two line points.
transform.LookAt(B);
Set “height” to Vector3.Distance(A, B)
Set radius to whatever best matches your LineRenderer’s visual width.
What is happening in 2,3 and 4th steps.How do you calculate ones position by adding two vector and divide by two?what is the need for LookAt?
For some reason the collider is not getting detected.Has it anything to do it with adding 3D collider and detecting using Physics2D raycaster in Main Camera? After adding a 3D box collider into a sprite renderer,it is not getting detected where as 2d box collider is getting detected.
But I have to add 90 to the rotation of x axis. to come align to it.I am drawing several lines so i made capsule collider prefab.When I instantiate it it is coming on different positions.
Yes Now the collider comes on the line renderer.How to check whether the touch is registered on the Line renderer.
Methods I tried was not successful.
RaycastHit hit;
// Does the ray intersect any objects excluding the player layer
if (Physics.Raycast(Camera.main.transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
{
Debug.DrawRay(Camera.main.transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
Debug.Log("Name is = " + hit.collider.name);
}
Next I tried IPointerHandler with Physics 2d Raycaster on main camera
if (eventData.pointerEnter.gameObject.name == "CapsuleColl(Clone)")
{
Debug.Log("Pointer on Line");
sound[3].source.clip = sound[3].clip;
sound[3].source.loop = true;
sound[3].source.Play();
}
The second method I am checking against the capsule collider having the game object called CapsuleColl(Clone) which was instantiated along with the line …if (eventData.pointerEnter.gameObject.name == “CapsuleColl(Clone)”)