- Home /
Script when i touch an object it must do something
Hi,
I'm making a game for android and now i have a problem. :( I have a lot of objects (all clones created during the game) very near together. And i need to do a script that do this: When i touch one object it must change his position, only the object i have touched and not all the near objects. I can't write a script that can "select" an object when i touch it. I'm using C# and i'm blocked at this point. If you need more information ask me.
Please, I need your help. Thanks.
I tried this
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (collider.Raycast(ray, out hit, 1000) )
{
move the object
}
}
But When i touch one of my clone (a prefab), all the near prefab change them position. I need a script that if i touch once only one clone need to change him position and not all the near prefab. (they are very very near).
Answer by leonalchemist · Oct 07, 2011 at 07:29 PM
i think a very simple OnTriggerEnter will work
void OnTriggerEnter (Collider Enemy)
{
if (Enemy.tag == "Player")
{
//something like
gameObject.transform.position = some Vector3 position
}
}
make sure the tag on ur charatcer i set to 'Player' and the collider 'is Trigger' is checked. Im guessing its a prefab so u can attach the script to the prefab, and the OnTriggerEnter will only change the position of the object it hit cause the OnTriggerEnter event only take effect to the object u've hit.
mmmm, it don't run. I don't know how to use this. But if all clones of prefab are set to Player, all of them change position, and not only once a time.
have u manage to make it work, normally only the Collided object will make the script work so the transform.position should only change the position of the gameObject the player got hit with, thats why OnTriggerEnter is quite useful.
but when u say "When i touch one object it must change his position" as in with the touchscreen the is it a character on screen that collides with other objects? if its the 1st then i wont be able to help u further cause i dont know nothing bout how touch screen works.
Yes, i want the object do this when i touch the screen (when i touch the object).
The problem is that when i touch with my finger i touch 2-3 objects and their change the position, but i want do: 1 touch = only 1 object change position.
Your answer
Follow this Question
Related Questions
cant touch multiple objects on screen (c# , mobile) 0 Answers
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Make Swipe Detection Longer? 0 Answers