touch gameobject in unity

 using UnityEngine; using System.Collections;  public class RaycastTest : MonoBehaviour {      Vector3 touchPosWorld;      //Change me to change the touch phase used.     TouchPhase touchPhase = TouchPhase.Ended;      void Update() {         //We check if we have more than one touch happening.         //We also check if the first touches phase is Ended (that the finger was lifted)         if (Input.touchCount > 0 && Input.GetTouch(0).phase == touchPhase) {             //We transform the touch position into word space from screen space and store it.             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);              Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);              //We now raycast with this information. If we have hit something we can process it.             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);              if (hitInformation.collider != null) {                 //We should have hit something with a 2D Physics collider!                 GameObject touchedObject = hitInformation.transform.gameObject;                 //touchedObject should be the object someone touched.                 Debug.Log("Touched " + touchedObject.transform.name);             }         }     } }

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source