- Home /
Question by
timeblade0 · Feb 06, 2017 at 08:30 AM ·
mouse drag
How to drag an object with mouse
I've used Gamemaker for years, but am now new to Unity.
I'm making a 2D game where a player drags a Pokeball with the mouse.
imported the sprite
dragged the sprite into the scene.
scene only has the camera and that sprite
when I hit play, the sprite is visible.
What script or setting would I use to drag the Pokeball with mouse? I want the center of the sprite to be at the cursor when dragged.
In gamemaker, I used:
pokeball object:
create event:
set = 0;
Step event:
if(mouse_x>x-(sprite_width/2)&&mouse_x<x+(sprite_width/2)) //within width of sprite
{
if(mouse_y>y-(sprite_height/2)&&mouse_y<y+(sprite_height/2)) //within height of sprite
{
if(mouse_check_button_pressed(mb_left)){set=1;}
if(mouse_check_button_released(mb_left)){set=0; }
}
}
//drag object
if(set==1){
x=mouse_x;
y=mouse_y;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612110429im_/https://answers.unity.com/themes/thub/images/avi.jpg)