Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by c.ovayurt · Jan 06, 2014 at 02:26 PM · android2dgameobjecttouch

Game Object On Touch Event on Android-iPhone 2D

Hi everyone,

I'm new at unity and trying to figure out how to detect touches on the gameObject. First of all I created a sprite sheet and put it into the material. For the game object I am using this material and making sprite animation with it.

Thus, basically I want to do something when someone touch the object on android phone.

-How can i detect the touches on the gameobject?

  • don't know how collider works do i need it for detect touches?

-Generally i'm using javascrips for the scripting. Do i have to use C# scripts ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

Answer by djonn · Jan 08, 2014 at 10:06 AM

-You can detect what was touched and send a message to the touched gameobject.

-You need colliders to detect touch. I simply use a box collider.

-You can use javascript.

I wrote this script:

 var platform : RuntimePlatform = Application.platform;
 
 function Update(){
     if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
         if(Input.touchCount > 0) {
             if(Input.GetTouch(0).phase == TouchPhase.Began){
                 checkTouch(Input.GetTouch(0).position);
             }
         }
     }else if(platform == RuntimePlatform.WindowsEditor){
         if(Input.GetMouseButtonDown(0)) {
             checkTouch(Input.mousePosition);
         }
     }
 }
 
 function checkTouch(pos){
     var wp : Vector3 = Camera.main.ScreenToWorldPoint(pos);
     var touchPos : Vector2 = new Vector2(wp.x, wp.y);
     var hit = Physics2D.OverlapPoint(touchPos);
     
     if(hit){
         Debug.Log(hit.transform.gameObject.name);
         hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
     }
 }


This works both in the editor (on windows at least) and on android+iphone.

It works by sending a message to the gameobject to run the function Clicked(), using a script on the gameobject that contains that function will make it run. It also logs the name of the gameobject for debug purposes.

This only works for 2d colliders.


Also, you don't need materials for displaying sprites.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image c.ovayurt · Jan 11, 2014 at 06:12 PM 0
Share

Thank you very much it Works!! I would like to up vote but i do not have enough karma to do it.

avatar image Qasem2014 · Dec 30, 2014 at 10:48 AM 0
Share

WOW

Very GOOD ;)

avatar image Pedrocosta2111 · Nov 03, 2015 at 05:08 PM 0
Share

Can you translate this var hit = Physics2D.OverlapPoint(touchPos); to c#?

avatar image djonn Pedrocosta2111 · Nov 03, 2015 at 05:09 PM 0
Share

Sorry, I don't know C#

avatar image iceblaze Pedrocosta2111 · May 06, 2016 at 12:44 AM 1
Share
 using UnityEngine;
 using System.Collections;
 
 public class OnTouch : $$anonymous$$onoBehaviour {
     private RuntimePlatform platform = Application.platform;
 
     void Update(){
         if(platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer){
             if(Input.touchCount > 0) {
                 if(Input.GetTouch(0).phase == TouchPhase.Began){
                     checkTouch(Input.GetTouch(0).position);
                 }
             }
         }else if(platform == RuntimePlatform.WindowsEditor){
             if(Input.Get$$anonymous$$ouseButtonDown(0)) {
                 checkTouch(Input.mousePosition);
             }
         }
     }
 
     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.transform.gameObject.Send$$anonymous$$essage("Clicked",0,Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
     }
 }
 
     

I hope it helps

avatar image obiolg · Jan 02, 2016 at 12:41 PM 0
Share

Trying to use exactly the same code, but hit would always be null. $$anonymous$$ay be you can help to figure it out? Having 2D project scene, a orthographic camera and a mesh (collider on, facing the camera). The values I receive are: pos: (516.0, 241.0), worldPos: (0.1, 0.0, -10.0), touchPos: (0.1, 0.0). Wondering why wouldn't Physics3D.OverlapPoint work.

avatar image
1

Answer by vickygroups · Apr 20, 2014 at 06:32 PM

Your game object needs a collider, and your Raycaster needs to be turned on, for touches to be detected. If you're new to programming, you might buy a plug in to assist you with the coding.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Graham-Dunnett · Jan 06, 2014 at 02:33 PM

Look at the last example on the documentation page:

http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html

All scripting in Unity can be performed with JS, c# or boo.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image c.ovayurt · Jan 06, 2014 at 02:59 PM 0
Share

Ok. I got the code but is there anything to do with the colliders or physics or gameobject inspector? No explanation was given on this site you shared. Also It is not the thing that i am looking for. I don't need to move the object according to my finger. I just need to detect the finger touch on the object or not :)

avatar image
0

Answer by JAHBLO · Sep 03, 2014 at 02:34 AM

Hi guys, I am trying to do the same thing but here is not working that well, I am able to drag the object by touching and dragging, but it won't hold for long, after a few milliseconds it falls Oo

I have tried many different approaches, same results, any help will be appreciated.

My code:

 Touch touch = Input.GetTouch(i);
         
 if (touch.phase == TouchPhase.Moved)
 {
                         
     Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
 
     Vector2 touchPos = new Vector2(position.x, position.y);
                             
     RaycastHit2D hit = Physics2D.Raycast(touchPos, Vector2.zero);
 
                         
     if(hit)
     {                                
         GameObject tObject = GameObject.Find(hit.transform.gameObject.name);
         tObject.rigidbody2D.isKinematic = false;
                                 
         position.z = 0; 
         tObject.rigidbody2D.transform.position = position;
         cam.GetComponent<MouseCamera>().enabled = false;
     }else{
         // enable the camera movement back                
         cam.GetComponent<MouseCamera>().enabled = true;    
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JAHBLO · Sep 04, 2014 at 09:28 AM 0
Share

Sounds good, thank you! @aldonaletto

avatar image AkooleGame · Nov 16, 2014 at 07:15 AM 0
Share

This is not an answer. Post a Question ins$$anonymous$$d.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform.LocalScale Script Problem. Please help. 2 Answers

Expand Object On Touch? Someone please help me 1 Answer

[Edited]Swap position of two Objects for Android on touch 2 Answers

Using raycasts to move a game object toward touch point. 0 Answers

Collision On Specific Object= Destroy not working. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges