0
Under review

How to: Spawn on mouse position.

Akeos 6 months ago in Game Creator updated by Marti (Lead Developer) 5 months ago 8

Hello. I need to spawn object on cursor position when i press ui button. Object should transform position with mouse cursor. 

Main problem how to find this cursor position.

I use triger ( on curcor raycast) on button with list variables ( vector 3 ) , but it doesnt work. What i doing wrong? 

Unity version:
2019.1.8f1
Game Creator version:
0.9.7
Answered

Excellent question. We actually struggled with the same for the upcoming RTS tutorial: Moving multiple characters as a unit where the user has clicked. To do so, we had to create a new Trigger that stores the position where the user has clicked. I'm attaching it to this post, but should be available soon at the Game Creator Hub.

mouseclick-position.unitypackage

To use it, simply place a Trigger onto the surface the user will be able to click (floor, for example) and select in which Variable you want to store the position.

Yes it works. But im about something different.. Im about:

1) mouse Btn down trigger+

2) instantiate Game object +

3) moving mouse => Game object moving to cursor all the time ( i about this part, more difficult) -

4) Mouse btn up trigger => instantiate Game object.

Under review

Hi Akeos. This is not yet possible then. If you want the object to move where the instantiated object while the mouse is being held down, you would need to create a custom Action (if you know a bit about how to code in C#, we can help you).

As a headstart, you'll need some sort of entity that checks for a boolean variable (let's say "dragging"). If it is true, means the mouse is being held down and it should raycast from the camera towards where the mouse is, in every frame. You can use the position returned by the collision to move the instantiated object towards that position.

+1

i did it like that.

its a script for GameObject ("Target"), which moving to cursor whole time, above (or under) ground / plane (need a tag).

"Target" unactive and all the time on scene (i dont think what its a best solution).

Further on the mouse down trigger on ui button "Target" set active and moving to cursor above ground.

After trigger mouse up => instantiate Prefab.

Im newbie , so if its can be done more simple and logical way, pls tell me ^^

May be there is a way with cursor UI?

Video

script

Idk, may be it help someone, or GC in the future =)

Yeah that's perfect. In fact, keeping the "cursor" object inactive while the mouse is not pressed is the optimal solution. Otherwise, you'd get a performance overhead when instantiating the object every "On Mouse Down" event.

Cheers!

+1

I've been wanting an Action like this for a while. It'd be great if you can make it official so it can work with mobile and Inventory module. Essentially I want to drag and drop an object like in Bloons TD.

I also really need a basic grid system. I found this script, but have no idea how to make it work with GC. Of course, to use it I'd first need the above action to work.

Crispy for different questions, please, consider creating a new topic. But nonetheless, it's currently not feasible to create a grid system with the Inventory (or not without a ton of rework)