Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Fade gui in and out depending of player position near and away some Brick?

Asked by 6 years ago
Edited 6 years ago

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:

http://prntscr.com/kblcuq

Hugs brick:

https://prnt.sc/kblcz7

2 answers

Log in to vote
-1
Answered by
saenae 318 Moderation Voter
6 years ago

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
0
I did al ittle correction and it worked THank you so much ! Igoralexeymarengobr 365 — 6y
0
No problem! Happy to help. saenae 318 — 6y
Ad
Log in to vote
3
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

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! :)

0
Thank you, i think i got it , saved me a lot of time :)! Igoralexeymarengobr 365 — 6y

Answer this question