- Home /
Question by
lothlorientos · Jul 14, 2014 at 09:50 PM ·
touchraycastingraycasthitperspective camera
Raycast2D with touch in perspective camera.
I have this code:
using UnityEngine;
using System.Collections;
public class dice_roll : MonoBehaviour {
public AudioClip sound;
void Update () {
if (Input.touchCount == 1) {
var ray = Camera.main.ViewportPointToRay(Input.touches [0].position);
var hit = Physics2D.GetRayIntersection(ray);
if (hit.collider != null) {
audio.Play();
}
}
}
}
But the touch doesn't seem to work, because the sound is not playing.
Did I do something wrong with the code?
Comment
Anyone knows? Btw I'm using 2D colliders for the objects.
Best Answer
Answer by robertbu · Jul 15, 2014 at 04:26 AM
Input.touch[0].position is a Screen coordinate, not a Viewport coordinate, so line 10 should be:
var ray = Camera.main.ScreenPointToRay(Input.touches [0].position);
Your answer
Follow this Question
Related Questions
Multiple hit detection with Raycasting? 2 Answers
Touch isn't working properly. 0 Answers
Another Raycast Issue. 0 Answers
Raycast script help? 1 Answer
RaycastHit always returns 0 1 Answer