playMaker

Author Topic: Click to move  (Read 5156 times)

ArctheLad

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
Click to move
« on: February 11, 2013, 08:45:15 AM »
Hey forum,

Anyone able to shed some light on how to make an object move towards a clicked location? I've tried all sorts of things, and when I raycast the object doesn't move towards the clicked location at all. It simply falls into the ground.

Any help would be greatly appreciated.

It's very much like games such as Diablo 2, League of Legends. That sort of stuff.

kiriri

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
Re: Click to move
« Reply #1 on: February 11, 2013, 10:03:27 AM »
Hey there,

While it may sound simple, actual movement a lá Diablo is a rather complex thing within Unity. The keyword here is pathfinding, which is a system that has a look at you scene and computes a path from one position to another. But since that's rather complex and pretty much always involves plugins, let's just start with the basics.

To move from one position to another you take the target position and your current position. If you subtract your current position from your target position you get the so called direction value.
This direction value can be used in a number of actions, like the move by, translate (with every frame set to true) etc. Most actions ignore other gameObjects however. So to make sure your character does not walk through walls unity offers newcomers a premade "character controller" component. Once added to your gameObject, you can use the action "Simple move" to move in a direction. This also simulates gravity.

So now you can use "get position" to get your current position. Then you can use a "mouse pick" to determine the hit point of the current mouse position (= the point right under your mouse cursor). Then you can use the "vector3 operator" to calculate the direction vector. Then you can use that vector in the simple move action. If you turn every "everyFrame" to true, then this simple FSM should make yourcharacter follow your mouse cursor.

For actual pathfinding I highly recommend for you to get confortable with playmaker first.I'm writing an implementation for Aron's Astar Pathfinding at the moment, but it will still take some time and may just be rather complex.
Best,
Sven

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Re: Click to move
« Reply #2 on: February 11, 2013, 12:46:08 PM »
I did it originally by ray casting at the mouse click location and where it intersects with the floor, storing that vector3, using a look at and applying a speed variable to transform the Z axis (which was pointing forward, in the direction it just looked) until it reached the stored location and set the speed to 0.

Running every frame and it seemed to work ... decent, i guess.. Then I ran into physics/collision issues and had to go back to the drawing board. I want to try it again and envelope it into a more flexible package by sending the vector3 to the unit you have selected, but I need to get into a selection array system and stuff.. meh.. Not necessary for Diablo style movement but necessary for RTS style multi-unit movement and many different destinations.

I may be able to throw something quick together for you on Wednesday for this, depends on if I get time but I have something I want to work on that needs this so I plan to do it anyway. I want to include actual physics on the unit and set a way to identify if the unit is making progress in movement or if its stuck, thus stopping any glitchy movement.
« Last Edit: February 11, 2013, 12:51:14 PM by Lane »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Re: Click to move
« Reply #3 on: February 13, 2013, 05:54:52 PM »
Here you go.

This gives you a basic idea of how it can work.

ArctheLad

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
Re: Click to move
« Reply #4 on: February 14, 2013, 09:45:13 AM »
Cheers guys, really appreciate the help I can get from this community!