- Home /
Question about Unity Touch Interface For Android
I need help on unity's touch interface, I want to 'do something' when the user touches the gameobject (a cube), for example : load level 1 when user touches the cube.
Can anyone please help me ? Thank you For your time.
Answer by Peter G · Aug 14, 2011 at 03:21 PM
This should be fairly simple. Iterate through all the touches, ray cast for each one, and check if you hit anything:
function Update () {
var tapCount = Input.touchCount;
for (var i = 0; i < tapCount; i++) {
var touch = Input.GetTouch(i);
var hit : RaycastHit;
if (Physics.Raycast ( Camera.main.ScreenPointToRay(touch.position) , out hit ) ) {
//Do stuff with the `hit` information
}
}
}
Errors : " 1 . Assets/$$anonymous$$y Files/Scripts/EAi.js(6,85): BCE0044: expecting ), found 'hit'. on 6th line
2 . Assets/$$anonymous$$y Files/Scripts/EAi.js(6,91): BCE0043: Unexpected token: ). on 6th line
3 . Assets/$$anonymous$$y Files/Scripts/EAi.js(8,12): UCE0001: ';' expected. Insert a semicolon at the end. on 8th line "
How is this even possible ?
alright it did the job but whenever i touch near the cube it executes the 'do something' code in my case ( transform.gameObject.tag = "currentEnemy"; ) . I want that the user touches the cube not its near surroundings . and another thing how can i connect two scripts for example : Function update() { connectedscript.execute }
something like that. Thank you
edit : it also 'do something' when i rotate the camera ,,, :(
I don't have the time to answer all your questions, and many of them have been answered before so you should be able to find them. I will answer the first question though:
You need some conditional to make sure that when you hit the cube it returns true and every other time its false. You can compare the name or the tag or check if it has a given component attached (`GetComponent() != null`)
To access another script, you want to use either GetComponent()
or FindObjectsOfType()
generally to access another script. Then, when you have an instance of your script ready, you can call functions with it.
Your answer
Follow this Question
Related Questions
Touch Hold for Power 1 Answer
Adroid Input 1 Answer
InputManager vs iPhone/Android 2 Answers
2D Camera (Android, Iphone) 1 Answer
Making 2D On screen buttons for movement 0 Answers