I've been trying to make a script for a gui to fade in when the player gets close to one brick the closer, the more transparent and the reverse for when the player gets away, i failed at every try and could not find if there is a **fuction ** for that, I would appreciate some help... Example Photos:
Getting close to the Brick: http://prntscr.com/kblcnv
Almost touching it:
Hugs brick:
I'll just write up some pseudo for how I'd personally tackle this problem:
startFadingDistance = 10; while wait() do distance = (character.pos - brick.pos).magnitude; if(distance <= startFadingDistance) then ui.Transparency = 1 - (startFadingDistance-distance)/startFadingDistance; end end
One method you could use is constantly calculating the distance between the player and that part. You can do this by using the Player:DistanceFromCharacter
method which returns the distance between the player's head and the given position. You should use the part's position as the argument.
Essentially, you can set the transparency of the GUI to be the percentage distance the player is from a set max distance. Let me explain:
Say you want the GUI to start visibly fading in at a distance of 10 units away. We will call this value MAX_DISTANCE
. We can constantly calculate the distance between the player and the part using the method above, and name this value CURRENT_DISTANCE
. The transparency of the GUI will depend on how far away the player is compared to MAX_DISTANCE
.
For example, let's say the player is currently 5 units away from the part. The transparency of the part would be CURRENT_DISTANCE / MAX_DISTANCE
which is 0.5. When the player comes very close to the part (CURRENT_DISTANCE
approaches 0), the transparency also approaches 0 (0 / any number = 0). If CURRENT_DISTANCE >= MAX_DISTANCE
, then we can simply set the transparency to 1.
With this system, the player will have a dynamic fading GUI that will become more opaque as he approaches the part, and more transparent as he walks away, with max transparency being at your specified MAX_DISTANCE
.
Hope this helps! :)