playMaker

Author Topic: Changing color of object based on distance to another object?  (Read 6941 times)

frogwise

  • Playmaker Newbie
  • *
  • Posts: 49
Hey everyone,

I have a stationary object, and a moving player. I am checking the distance between player and object every frame. The object is black.

What I would like to do is have the object change to white based on how close the player gets to it. So, for example, the minimum distance for the object to start transitioning from black to white is 20, and a distance of 3 sets the color to 100% white. Once the distance is that close, the object stays white forever.

Any elegant solutions for this? Thanks so much in advance. :)

Arnoob

  • Junior Playmaker
  • **
  • Posts: 63
Re: Changing color of object based on distance to another object?
« Reply #1 on: May 04, 2015, 08:28:58 AM »
Hi Frogwise!

I don't think there is an "elegant"(aka simple) way to do that, but it's not that complicated tho.
Simply try to use a "get distance" action in the GameObject subsection. Then store the float value (don't forget to check "every frame") and use a "float compare" action to send an event to an other state when your float value is under a certain value.
Then in this new event, do an action to set a new texture as you want.

Hope this will help :-)

frogwise

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Changing color of object based on distance to another object?
« Reply #2 on: May 04, 2015, 11:28:47 AM »
Thanks for the reply.

I'm actually more confused about how to smoothly fade between two colors based on that distance.

For example, at a distance of 5 it starts to fade to the new value based on how close you get. If, for example, you get further away again, it starts to fade back to the original color.

I could do this with a Color Ramp, but the color ramp will only work between 0-1 values, or 1 and 2. I don't know how to map my distance values in a normalized way like this.

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Changing color of object based on distance to another object?
« Reply #3 on: May 04, 2015, 12:56:58 PM »
There's a "Remap float " action that Jean made that could help you with this http://hutonggames.com/playmakerforum/index.php?topic=5122.msg24311#msg24311
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Changing color of object based on distance to another object?
« Reply #4 on: May 04, 2015, 12:59:10 PM »
The Remap-thing sounds good.
But remember that 0-1 values are basically percentages divided with 100.
If you have your two outer distance values (5 and 0 -- and 5 being 100%) you could find the percentage based on the current distance -- every frame, and set the color value based on that.

/Bad
The Force is with you, young Playmaker – but you are not a C# senpai yet.

frogwise

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Changing color of object based on distance to another object?
« Reply #5 on: May 04, 2015, 04:57:06 PM »
Thanks for opening up my eyes to the power of division! It's working now, however, I am noticing that the transition of the colors is very fast. They do "fade" into each other, but very quickly. Is there a way I could curve the float to be less sensitive to transition at the maxDistance, and faster at the minDistance? Here is my setup that works currently.


frogwise

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Changing color of object based on distance to another object?
« Reply #6 on: May 04, 2015, 05:03:27 PM »
I guess the problem I am having is that for some reason, a now-normalized distance value of 0.5 is not producing a good halfway point blend between my two colors. For some reason, by a distance of 0.9, I am already pretty much at my target color. This is why I am seeing such a quick transition.

frogwise

  • Playmaker Newbie
  • *
  • Posts: 49
Re: Changing color of object based on distance to another object?
« Reply #7 on: May 04, 2015, 05:33:54 PM »
Fixed it! The problem was that the color variable "color_current" was being used incorrectly. I was using it to grab the initial materials color, and then in the Color Ramp using the same variable to store the ramping color. I made a third color variable and used that. Works like a charm.


TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Changing color of object based on distance to another object?
« Reply #8 on: May 04, 2015, 05:52:40 PM »
Whoups, just sent you a reply on your PM, didn't see your progress here.
Looks like you solved your riddles.

Good work.
/Bad
The Force is with you, young Playmaker – but you are not a C# senpai yet.