- Home /
Select an object with touch (Android)
Hi. I want to get an object's name, by tapping it on an Android touchscreen, then pass it to another script.
I use this script:
function Update () {
if(Input.touchCount > 0)
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
anotherScript.whattomove = hit.rigidbody.gameObject.name;
}
}
}
I checked and Unity knows when i touch the screen, but this doesn't work. Any help would be appreciated. Thanks
Comment
Answer by Bazsee · Apr 12, 2012 at 11:12 AM
I found the error, Camera.ScreenPointToRay needs a Vector3 argument, but Input.GetTouch(0).position returns a Vector2, so a little trick is neccessary.
function Update () {
if(Input.touchCount > 0)
{
var hit : RaycastHit;
var vektor = Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0f);
var ray : Ray = Camera.main.ScreenPointToRay (vektor);
if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
anotherScript.whattomove = hit.rigidbody.gameObject.name;
}
}
}
Your answer

Follow this Question
Related Questions
Android touch 3d Object event 1 Answer
How do I check if an object is touching another object? 1 Answer
What's the matter with this script? Detect object dragging with iPhone Touch 1 Answer
Destroy object if get touched 1 Answer
NullReferenceException: Object reference not set to an instance of an object 0 Answers