- Home /
Starting and Ending points of LineRenderer
Hello Everyone
Currently I'm working on a Demo which has similarities between a game https://modularmindset.itch.io/cardrawinggame-workingtitle.
I'm working on Unity3D and I draw lines with using LineRenderer sadly I'm not familiar with the concept so I have some problems.
When Each time player draws a line, I want to Instantiate wheels of the car in starting and ending points, How can I find Starting and Ending points of Line that I drew ?
Also after Instantiate how can I move the line with using tires ?
Thank you for even smallest answer.
Answer by lgarczyn · Oct 04, 2019 at 11:10 PM
This is a complex question, but not the part that you are thinking of.
To get the start and end positions, you just need to do this:
Vector3 startPos = line.GetPosition(0);
Vector3 endPos = line.GetPosition(line.positionCount - 1);
This will give you the local position (and world positions if they don't have parents) of the start and end point. This will crash if the line doesn't have points however. For this to work you need to disable useWorldSpace, or convert the result to local space.
Not if you want it to move, you will need to:
add a 2d rigidbody to your linerenderer
create a prefab for wheels, which will require a 2d circle collider, and a wheel joint. More details here. You will need to instantiate two of these prefabs, and attach them to your linerenderer, then set their ConnectedAnchor to startPos and endPos.
I suggest you try to do it in the editor before trying to code it.
This should give you a car, but the line won't collide with anything. For that, you will need to extract all the points of the lineRenderer, and add them to a PolygonCollider2d
. Another option is to use the BakeMesh
function from the LineRenderer and use this as the input for the polygon collider, but you'll need to convert it by hand.
There are other people who worked on this though, as you can find here:
https://answers.unity.com/questions/768997/how-can-i-add-a-collider-to-my-line-renderer-scrip.html
$$anonymous$$aybe I can do it in a different way, I can Instantiate wheels where I clicked mouse and Where I lift my finger it will be start and end position of the line ?.
Oh, yeah that works. Won't help for the next part though.
Your answer
Follow this Question
Related Questions
3d object outline shader 1 Answer
Draw path along Navmesh agent path 3 Answers
How to check of a line renderer collides with ground ? 1 Answer
How to drag and drop lines in-game 1 Answer
Reflecting LineRenderer in water 0 Answers