- Home /
Raycast for 2d with touch.
I want when a player touches a certain object in my 2D game to play a sound.The reason I'm using raycast is because I'm working with a perspective camera instead of orthografic and when I use basic touch detection it doesn't work, so I use this method. But, with this code I need a 3d collider which I don't like, since my game will be 2D so I would be using unnecessary cpu time I guess. The code is the following:
 using UnityEngine;
 using System.Collections;
 
 public class dice_roll : MonoBehaviour {
     public AudioClip sound;
     private RaycastHit hit;
     private Ray ray;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.touchCount == 1) {
                         ray = Camera.main.ScreenPointToRay (Input.touches [0].position);
                         Debug.DrawRay (ray.origin, ray.direction * 10);
                 }
             if(Physics.Raycast(ray.origin, ray.direction * 10,out hit)){
                 Debug.Log(hit.transform.name);
             if(hit.collider.gameObject.tag == "dice"){
                 
                 audio.Play();  
                 
             }
 
             }
         }
 
 
 }
So how can I change this code to work for 2d collider?
               Comment
              
 
               
              Anyone? If you need something clarified more tell me :)
I don't think you have to worry about CPU cost. Does your code as it is today work? If so, just move on.
yes it works I just wanted to see if there was a better way...
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                