Hi!! I’m trying to send a Raycast from the position on which the finger touch the screen but until now i have failed completely ![]()
Any tips/help/NonUnityofficial documentation that you can spare with me?
It is a 2D game and the future idea is to grab a position and send a gameobject to it … like a touch-to-move kinda game. (It is not a sidescroller)
What i have now:
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
RaycastHit hit;
if ( Physics.Raycast(ray, out hit))
{
Debug.Log("something is hitted");
}
}
Pretty basic code but it doesn’t work … is it because it’s a 3D code on a 2D enviroment(?)
Oh well Thanks! =) xP :Ankward moment:
– MaenorHey @DiegoSLTS I added the correct fixes as you indicated .. but i got some errors .. and after a little research i discovered that i must pass it a Vector2 argument ... Where should it be? Right now i have it on if (Physics2D.Raycast(ray, out hit, Vector2.zero) { } But that gives me errors ... I also tried with the vector2 argument outside of the brackets but it's the same ...
– MaenorNo worries!! I got it!! xP Here the entire script!! xO void Update(){ if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero); if(hit.collider.tag == "//someObject"){ Debug.Log("//someObjectisHitted"); Debug.DrawLine(Input.GetTouch(0).position, hit.point, Color.green, 50); } }
– Maenor