- Home /
How to Raycast on Touch?
Greetings,
I currently raycast for each touch in each frame.I only need to raycast once the touch is detected. Is there something like OnTouchDown?
Thanks!
Answer by HarshadK · May 04, 2015 at 01:30 PM
Use TouchPhase.Began
Usage example: Input.GetTouch
Does TouchPhase.Began work for mul$$anonymous$$ch too? When I try it it just cancels the previous touch. And yes I can support 5 touches.
Yes it can support mul$$anonymous$$ch. In the example usage link above just replace 0 to any other touch index in GetTouch()
RaycastHit2D hit;
Vector2[] touches = new Vector2[5];
void Update ()
{
if(Input.touchCount > 0)
{
foreach(Touch t in Input.touches)
{
touches[t.fingerId] = Camera.main.ScreenToWorldPoint(Input.GetTouch(t.fingerId).position);
if(Input.GetTouch(t.fingerId).phase == TouchPhase.Began)
hit = Physics2D.Raycast(touches[t.fingerId], Vector2.zero);
if(hit.collider.name == "Tri(Clone)")
hit.transform.position = touches[t.fingerId];
}
}
}
I have this, however only one touch works...
Just perform some debug logs to check if it is the touch that is not working or it is the issue with the Raycast.
Answer by DiebeImDunkeln · May 04, 2015 at 01:34 PM
if you want to check the touch on an object, use this:
void Update()
{
if (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount > 0 && Input.touchCount < 2)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
checkTouch(Input.GetTouch(0).position);
}
}
}
else if (platform == RuntimePlatform.WindowsEditor || platform == RuntimePlatform.OSXEditor)
{
if (Input.GetMouseButtonDown(0))
{
checkTouch(Input.mousePosition);
}
}
}
private void checkTouch(Vector3 pos)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if(hit && hit == gameObject.GetComponent<Collider2D>())
{
// do something
}
}
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount > 0 && Input.touchCount < 2)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
checkTouch(Input.GetTouch(0).position);
}
}
}
else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
checkTouch(Input.mousePosition);
}
}
Thanks so much for the answer. It helped me a lot. I was search for this past one week. The only change with your script is if (Application.platform == RuntimePlatform.Android) instead of platform == as per 2019 version
Answer by codemaker2015 · Oct 05, 2020 at 12:08 PM
void Update () {
if ((Input.touchCount > 0) && (Input.GetTouch (0).phase == TouchPhase.Began)) {
Ray raycast = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
RaycastHit raycastHit;
if (Physics.Raycast (raycast, out raycastHit)) {
Debug.Log ("Something Hit " + raycastHit.collider.name);
switch(raycastHit.collider.name) {
case "Level1": Debug.Log("Level1 selected"); break;
case "Level2": Debug.Log("Level2 selected"); break;
case "Level3": Debug.Log("Level3 selected"); break;
case "Level4": Debug.Log("Level4 selected"); break;
case "Level5": Debug.Log("Level5 selected"); break;
default: Debug.Log("Wrong level selection");
}
}
}
}
Your answer
Follow this Question
Related Questions
Touch interface not working 1 Answer
Button Focus Prevents Touch Raycast (Android C#) 1 Answer
Unity touch input 1 Answer
How to make object face touch position 1 Answer
Paddle script for Pong-based video game for Android 0 Answers