- Home /
itween: targeting differnt gameobjects after mousecklick
Hey guys. Iam trying to archive the following thing:
i have 2 boxes and one character which is controlled with the third person controller in my game. and all i want to do is, that the character will move automaticly to the box when i clicked it.
i tried it with itween (visualEditor). works verry well with one box. but only when marked it as "play automaticly" because i have no idea how i call the itween function only when i click one box. so, this would be my first question:
how can i call the itween function when i click on the box?
second question: i have two boxes. and at the moment, the itween script is put onto the player. so that means if i put 20 boxes in the game. there have to be 20 itween scripts in the player object? isnt there a better solution for this?
thanks for help
How are you detecting when the player has clicked on the box? By a Raycast from the camera or in a 'On$$anonymous$$ouseDown' function in a script that is attached to the box?
Answer by DannyB · Jul 20, 2013 at 03:12 PM
You do not need 20 iTween scripts, and you do not need to use the visual iTween editor.
Assuming you know how to communicate between two different game objects and to send variables between them, the basic logic for such implementation is simple but may vary depending on your situation.
In general: The player object should listen to tap events on the box, when any box is tapped, the player decides what to do with it.
You can implement it with C# events, with Unity's SendMessage
, or using publicly exposed methods on the player.
Example for the latter:
// In player script
public void OnBoxTap( GameObject box ) {
iTween.MoveTo( gameObject, iTween.Hash(
"position", box.transform.position,
"time" , 1
));
}
// In box script
public PlayerClass player;
void OnMouseDown() {
player.OnBoxTap( box );
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move object from A to B on sphere 2 Answers
How do you create random paths in iTween? 0 Answers
help with target in my fps game ? 0 Answers
Move player to specific location and avoid obstacle. 2 Answers
iTween for Dynamically Moving Objects around a Sphere 0 Answers