Question by
IbrahimK · Oct 11, 2015 at 10:09 PM ·
raycast2d-physics
Touch - Linecast collision detection
Hey guys,
I'm trying to implement a functionality, that detects an intersection(collision) between a swipe and a GameObject in 2D. Kinda like in the game Cut The Rope. So I have multiple GameObjects in my scene. And when I swipe I want to record the first GameObject that I intersect with. My code right now looks like this:
public GameObject obj;
private Vector2 pos1;
private RaycastHit2D hit;
void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
pos1 = Camera.main.ScreenToWorldPoint(touch.position);
break;
case TouchPhase.Ended:
RaycastHit2D hit = Physics2D.Linecast(pos1, Input.GetTouch(0).position);
if (hit.collider)
{
obj.GetComponent<Text>().text =
obj.GetComponent<Text>().text + hit.collider.name;
}
break;
}
}
}
The problem is, that this doesnt seem to be accurate and it doesnt work, if I swipe vertically. What am I doing wrong? I'm not even sure, if this is the right approach :S.
Comment
Your answer
Follow this Question
Related Questions
2d object moves only a little then stops 0 Answers
Hookshot in Unity 2D not working,Hookshot 2D not working 0 Answers
Raycast not detecting bullet 0 Answers