playMaker

Author Topic: How to push away 2d object based on mouse position?[SOLVED]  (Read 245 times)

rechronicle

  • Playmaker Newbie
  • *
  • Posts: 43
  • Medievaliant
    • View Profile
    • Indie RPG Creator
How to push away 2d object based on mouse position?[SOLVED]
« on: November 05, 2018, 07:55:16 AM »
Hello,
I was wondering how to make this:
if the mouse were above the object, and you click it, the object will move downward.
if the mouse on the left, the object will move to the right
the nearer the mouse position to the object, the stronger the power.

Can't seem to get it right, the object can only move to the right in my attempts. If you know how to do it, please help!
Thank you!

« Last Edit: November 23, 2018, 12:24:11 AM by jeanfabre »

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 987
    • View Profile
    • Fat Pug Studio
Re: How to push away 2d object based on mouse position?
« Reply #1 on: November 05, 2018, 08:08:34 AM »
I'm not on my desktop right now, but i do have a question. Do you need it to move strictly up/down/left/right, or should be something like if you click upper left of it, it will move lower right?

rechronicle

  • Playmaker Newbie
  • *
  • Posts: 43
  • Medievaliant
    • View Profile
    • Indie RPG Creator
Re: How to push away 2d object based on mouse position?
« Reply #2 on: November 05, 2018, 08:19:28 AM »
Right now I'm aiming to make this case: if the mouse on the upper-right then the object move to lower-left.

But,, learning about the method to actually limit the object movement on horizontal/vertical movement by mouse position is really interesting too! (If you don't mind sharing.) :D :D

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 987
    • View Profile
    • Fat Pug Studio
Re: How to push away 2d object based on mouse position?
« Reply #3 on: November 05, 2018, 08:33:03 AM »
Well, get the position of mouse click with position of the object (get x and y position float). Then you can compare y position of the click with position of the object. If y of the click is greater than objects, apply force down, if it's lower, apply force up.

On a separate FSM you can have the same for x axis, so the object will get vertical and horizontal force from both FSM and move appropriately.

To tune the amount of force, you can clamp it and to be more precise you can play around with remapping float value.

Or there's a complicated solution getting the vector2, getting the distance between object vector and click vector, then invert it and apply force, but it's complicated and i believe the first one is easier to grasp and to use.

rechronicle

  • Playmaker Newbie
  • *
  • Posts: 43
  • Medievaliant
    • View Profile
    • Indie RPG Creator
Re: How to push away 2d object based on mouse position?
« Reply #4 on: November 06, 2018, 03:21:09 AM »
Hey there,
I finally able to compare the position of the mouse and the object. Tried the first method.

The problem I got: 'Get mouse X' position is related to the screen size. Meanwhile, the 'Get Position' is related to the starting position/the world itself, which make the comparison show weird result.

How to work around with it?

EDIT: Nevermind, I found the 'Get Game Object Screen Position' action in the Ecosystem.

Thank you!
« Last Edit: November 06, 2018, 03:43:22 AM by rechronicle »