- Home /
How to move an image by tocuhing a button (using UI) [Android game]
Hi! I am fairly new using unity. In this short time that I've been using this software, I have been learning a lot. But, there are several primordial things that I can't understand yet. Im trying to make an android game in 2d and I just want to move an object to an static position by pressing a button (below this description I made an ugly scheme explaining my purpose).
I learned how to set sprites/images in the screen using the UI Canvas tool (how to set the position, how to set the size...). I understand too, how the default methods works (start and Upload). But I can't realize this things: -How to make a function that will execute at the moment that the button is touched (I suppose that we can't use the Upload function because it will not occurs always). -How can I make a function (from a script that belongs to this button) move another object.
I made a script for the object that I want to move that has a new function where I change the position of the object itself (something like transform.position=new vector3(-200,0,0); a static position... but I dont know how to call this function from the "button event" and if im making it in the correct way).
In this scheme you can see my purpose. After touching the "aButton" the black object (it can be a sprite or anything) must move to (-200,0) position.
Thanks for reading my question and sorry for my bad english.
Answer by Kastenessen · Jan 15, 2015 at 05:13 AM
Well, all I do is have a 2d collider on the button and use a raycast check and a on mouse button down click to activate a static bool that is accessable by the thing you want to move.
So once the bool is down it will move. Put the bool in the update function and make another bool also to switch its movement bool off and on, so you can stop it from moving again when you want to.
Your answer
Follow this Question
Related Questions
GoogleVR Sprite2D and Buttons Flickering/Glitch and FPS Canvas on one Screen 0 Answers
I can't change the sprite using the Touch class 0 Answers
Problem with touch button (when moved) 2 Answers
how use specific for mobile button when we have 3 ? 1 Answer
I am making android fps and my raycast shooting button don't work. What I should do? 1 Answer