playMaker

Author Topic: How to detect if distance is increasing or decreasing  (Read 2047 times)

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
How to detect if distance is increasing or decreasing
« on: January 14, 2018, 12:23:17 PM »
Hello,

What's the best way to detect if the distance between two objects is increasing or decreasing?

The idea I have is to take distance at time 1 distance at time 2, then subtract distance 1 from distance 2. If the result is positive, the target object is moving away from the player. If the result is negative, the target object is moving toward the player.

The result determines which event is sent next. I'm working on a script to do this, but I'm pretty new at C#, so I wanted to get your ideas first. Any advice is appreciated.

Edit: I'm messing around with Float Operator, Float Compare, and Float Sign Test, but I can't seem to get them to function with the two Get World Distance variables I've created.
« Last Edit: January 14, 2018, 01:14:37 PM by picklesandtickles »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: How to detect if distance is increasing or decreasing
« Reply #1 on: January 14, 2018, 04:53:19 PM »
Hi.
Maybe something like :

State 1 'get distance' and store in "Last Distance"
State 2 "Next Frame event"
State 3 'get distance' and store in "Current Distance"
State 4 'Float Compare, if lesser (closer) / if greater (farther) if equal (depends on you)

Then do what you need to do for Lesser/Greater on the next states and then loop back to state 1

picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: How to detect if distance is increasing or decreasing
« Reply #2 on: January 14, 2018, 07:17:15 PM »
Hi,

Thanks for the quick response. I moved some things around and tried your suggestion. Here's what I've got: https://imgur.com/a/T3UNw

The first picture shows what happens during play. The fsm constantly loops back around to the start, I think because it detects that lastDistance and currentDistance are equal.

Something strange, though, is that I threw in a Debug.Log to see if I could get the distance to at least show up in the console and it didn't print. Not quite sure why.

I stumbled across "Get Last Pointer Data Info" in Ecosystem. Could it be used to accomplish why I want? It has bools for "Is Pointer Moving," "Dragging," and "World Position." Maybe I can use this to accomplish something similar. If the target object is being dragged toward the player then X. If it is being dragged away from the player then Y. Any ideas around that?

Thanks again for your input.

Edit: Interestingly, the distance does show up in the Scene, as you can see in the attached picture, so I know that part is working.
« Last Edit: January 14, 2018, 07:38:47 PM by picklesandtickles »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7614
    • jinxtergames
Re: How to detect if distance is increasing or decreasing
« Reply #3 on: January 15, 2018, 02:09:43 AM »
Hi.
The get distance is not needed to be set to every frame.
Also be sure to have the right gameobject and target :)
I tested this @ a velocity of 0.1 and seems to be working :)

I made a gif showing how i would set this up.






picklesandtickles

  • Playmaker Newbie
  • *
  • Posts: 13
Re: How to detect if distance is increasing or decreasing
« Reply #4 on: January 15, 2018, 02:33:34 PM »
Awesome - thanks for setting it up and creating the gif!! I was able to quickly replicate it exactly, and it worked as advertised. :)

I'm hoping you can provide one more piece of advice related to this question. (edit: if you'd rather me just mark this as solved and open the question below in a new thread, I'd be happy to do that.)

I'd like it to constantly detect whether or not the Target object is moving towards or away from the Player object. To this end, I created a bool to store the left click followed by a bool test set to every frame.

When I do this, the Distance Manager seems to be constantly working as I move the target around the screen, but the distance that it detects seems to be more random. Can you think of a better way to set up the constant detection while dragging? Here's a video where you can see the behavior in the FSM:

https://youtu.be/d7JfgV4UWms

Thanks again for your help.
« Last Edit: January 15, 2018, 02:36:40 PM by picklesandtickles »