- Home /
Question by
pkdesigners · Nov 04, 2016 at 08:27 AM ·
unity 5linerenderer
Intersection between Line Render and image
Hi, I am trying to make word search game. I make line render script but i want detect line intersecting the image.
Here is code using UnityEngine;
public class DrawLine : MonoBehaviour { private LineRenderer _lineRenderer; public void Start() { _lineRenderer = gameObject.AddComponent();
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
_lineRenderer.sortingLayerID = spriteRenderer.sortingLayerID;
_lineRenderer.sortingOrder = spriteRenderer.sortingOrder;
_lineRenderer.SetWidth(0.2f, 0.2f);
_lineRenderer.enabled = false;
}
private Vector3 _initialPosition;
private Vector3 _currentPosition;
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
_initialPosition = GetCurrentMousePosition().GetValueOrDefault();
_lineRenderer.SetPosition(0, _initialPosition);
_lineRenderer.SetVertexCount(1);
_lineRenderer.enabled = true;
}
else if (Input.GetMouseButton(0))
{
_currentPosition = GetCurrentMousePosition().GetValueOrDefault();
_lineRenderer.SetVertexCount(2);
_lineRenderer.SetPosition(1, _currentPosition);
}
else if (Input.GetMouseButtonUp(0))
{
_lineRenderer.enabled = false;
var releasePosition = GetCurrentMousePosition().GetValueOrDefault();
var direction = releasePosition - _initialPosition;
Debug.Log("Process direction " + direction);
}
}
private Vector3? GetCurrentMousePosition()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var plane = new Plane(Vector3.forward, Vector3.zero);
float rayDistance;
if (plane.Raycast(ray, out rayDistance))
{
return ray.GetPoint(rayDistance);
}
return null;
}
}
Comment