- Home /
access gameobject via touch
how to access a gameobject via touch. it should display a text while touching!! please help me..
Answer by whydoidoit · Sep 14, 2012 at 08:53 AM
You need to perform a ray cast from the finger and test against a collider attached to the object. There is this script on the wiki that shows you how to do it - but be aware the script as it stands just detects a tap because is only operates during TouchPhase.began - you could easily modify it to check all of the valid phases for continual touch (TouchPhase.move and TouchPhase.stationary).
@mike: iwant for android ....please can you help me ?
Sure - so put this in a script attached to the camera:
void Update()
{
foreach(Touch t in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(t.position);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
switch(t.phase)
{
case TouchPhase.Began:
hit.collider.Send$$anonymous$$essage("OnTouchDown", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
hit.collider.Send$$anonymous$$essage("On$$anonymous$$ouseDown", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
hit.collider.Send$$anonymous$$essage("OnTouchOver", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
hit.collider.Send$$anonymous$$essage("On$$anonymous$$ouseOver", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
break;
case TouchPhase.$$anonymous$$oved:
case TouchPhase.Stationary:
hit.collider.Send$$anonymous$$essage("OnTouchOver", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
hit.collider.Send$$anonymous$$essage("On$$anonymous$$ouseOver", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
break;
default:
hit.collider.Send$$anonymous$$essage("OnTouchUp", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
hit.collider.Send$$anonymous$$essage("On$$anonymous$$ouseUp", null, Send$$anonymous$$essageOptions.DontRequireReceiver);
break;
}
}
}
}
Then the object being touched will get OnTouchDown/OnTouchOver/OnTouchUp functions called on it as well as On$$anonymous$$ouseXXXX functions for the fingers.
I guess you want to display the text while you get OnTouchOver.
@mike : iam getting an compilation error for this :( it says you cannot add this script to camera because it is not compiled.... isthere any value for t ?? pls do this favour for me ...
Your answer
Follow this Question
Related Questions
How do i detect if a certain cube is being touched? 6 Answers
How to get PointerEventDatas[]? 0 Answers
Move 2 gameObject with touch at the same time 0 Answers
Unity crashes on GameObject = hit.collider.gameObject; 1 Answer
Drag-and-Drop GameObjects 5 Answers