- Home /
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 ?
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.
Thank you very much it Works!! I would like to up vote but i do not have enough karma to do it.
Can you translate this var hit = Physics2D.OverlapPoint(touchPos); to c#?
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
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.
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.
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.
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 :)
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;
}
}
This is not an answer. Post a Question ins$$anonymous$$d.
Your answer
Follow this Question
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