# How do I chenge opacity depending on a distance?

Get help using Construct 2

### » Mon Jun 06, 2016 4:59 pm

Hi,

I'm experimenting with opacity change depending on a distance, and would want to do it between couple of ranges, like 1000 an 1100, where at instance of 1000 would give 100 opacity and 1100 would give 0 opacity. So i tied using clamp, and tried some to other ideas, but don't know what would be the easiest solution for that. So if anyone would have a suggestion, I'd appreciate it.
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
93
S
30
G
22
Posts: 1,987
Reputation: 20,203

### » Mon Jun 06, 2016 5:15 pm

clamp((1100 - distance(x,y,x,y)),0,100)

distance = 1100 > 1100 - 1100 = 0
distance = 1000 > 1100 - 1000 = 100
distance = 2000 > 1100 - 2000 = -900 > clambed on 0
distance = 500 > 1100 - 500 = 600 > clambed on 100
distance = 1050 > 1100 - 1050 = 50
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Mon Jun 06, 2016 5:26 pm

For a generic linear interpolation formula you can use lerp(100,0, clamp((distance - distanceForFullOpacity) / (distanceForFullTransparency - distanceForFullOpacity), 0,1)).
B
81
S
33
G
27
Posts: 1,029
Reputation: 21,143

### » Tue Jun 07, 2016 12:29 am

Thanks guys.

with this lerp(100,0, clamp((distance - distanceForFullOpacity) / (distanceForFullTransparency - distanceForFullOpacity), 0,1)) after filling it up it didn't work properly, untill I've switched places for the first two numbers ( so from 100,0 to 0,100) and it worked. Thanks!
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
93
S
30
G
22
Posts: 1,987
Reputation: 20,203

### » Tue Jun 07, 2016 1:30 pm

Oops! Must have inverted some numbers along the way.
B
81
S
33
G
27
Posts: 1,029
Reputation: 21,143