- Home /
Please help me achieve this
red line is where raycast direction point.
green circle is where mounseinput is
i want to draw a line like that or where the raycast direction is hitting the same object on intersection.
i already have a raycast matching object with same tag then destroy.
what i want is i wanted to draw a line the pointing to the direction like raycast.
Answer by LCStark · Oct 04, 2018 at 04:50 AM
Use a LineRenderer component. Create an empty object in your hierarchy, add a LineRenderer
component to it. When you want to draw a line, set the LineRenderer.positionCount
to 2, then use LineRenderer.SetPosition
or LineRenderer.SetPositions
to set the position of your start point and end points.
can you please give me a psuedo code ? or atleast an idea how i am going to write it.
Check out this manual page on how to create a line renderer in the Unity editor: LineRenderer.
In your code, where you do the raycasting, use your ray.origin
as the start point, and hit.point
as the end point. It's really just passing them as arguments to the line renderer's SetPosition
method:
public LineRenderer lineRenderer; // attach your LineRenderer object in the editor
Ray ray;
RaycastHit2D hit;
...
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, ray.origin);
lineRenderer.SetPosition(1, hit.point);
ray = Camera.main.ScreenToWorldPoint (Input.mousePosition); hit = Physics2D.Raycast (ray , Vector2.zero);
clickRight = Physics2D.Raycast (ray , Vector2.right);
clickTop = Physics2D.Raycast (ray , Vector2.up);
clickBottom = Physics2D.Raycast (ray , Vector2.down);
clickLeft = Physics2D.Raycast (ray , Vector2.left);
if (clickRight.collider.gameObject.tag == tiletag4 &&clickBottom.collider.gameObject.tag == tiletag4) { Debug.Log ("bottom right");
lineRenderer.positionCount = 3;
lineRenderer.SetPosition(0, ray);
lineRenderer.SetPosition(1, clickBottom.point);
lineRenderer.SetPosition(2, clickRight.point);
lineRenderer.enabled = true;
Debug.DrawLine(ray, clickRight.point, Color.red);
Debug.DrawLine(ray, clickBottom.point, Color.red);
game$$anonymous$$anager.blockCounter (2);
Destroy (clickBottom.collider.gameObject);
Destroy (clickRight.collider.gameObject);
audioSource = GetComponent<AudioSource>();
audioSource.clip = Scored;
audioSource.volume = _volume;
audioSource.Play();
}
how to make the line renderer show only for 1 frame.
Your answer
Follow this Question
Related Questions
Rendering streamline data 0 Answers
Issue drawing lines between buttons - Lines overlap the buttons 2 Answers
Is there anyway to draw smooth 2d lines? 0 Answers
Drawing Pixelated Lines 0 Answers
inconsistent lines errors?! 2 Answers