- Home /
Detect If Object Is Within Range Of Player To Change Object Opacity
I'm brand new to Unity and have barely any scripting experience so if you could just give me a strait script that would be great!
I'm making a Slender game.
I've got a cube that if infront of my camera and it is completely transparent. I want this cube to gradually become opaque the closer slender is (example: when Slender is distanced 10 away from the player than the cube is 15% opaque, 7.5 away 35%, 5 away 50%, 2.5 away 80% and 1.5 away 100%). So basically I want the closer Slender gets the more opaque the cube gets.
My Cube Tag Is "StaticObject"
My Slender Tag Is "SlenderMan"
My Player Tag Is "First Person Controller"
Hope you guys understand my question.
Cheers
Answer by brycedaawg · Jan 21, 2014 at 04:38 PM
Here is a script, written in C-Sharp, that will set a GameObject's transparency to a value that depends on how close it is to a target GameObject:
using UnityEngine;
using System.Collections;
public class OpacityController : MonoBehaviour
{
public GameObject target;
public float minDistance = 1.0f;
public float maxDistance = 10.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
float alpha = Mathf.InverseLerp(maxDistance, minDistance, Vector3.Distance(transform.position, target.transform.position));
renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, alpha);
}
}
Create a new C-Sharp script in Unity and name it OpacityController. Double-click it to open it in your editor. Replace it's contents with the above script. Back in Unity, attach this script to your cube (the one that represents your 'Slender Man'). Click the cube and, in the inspector, drag the GameObject that represents your character (The one that you wish Slender to become more opaque for the closer it gets) into the slot marked 'target'. Finally, you'll need to give Slender a material that supports opacity. In your assets, create a new material and then, in it's inspector, change the shader it uses to 'Transparent/Diffuse'. Drag this material onto Slender and you should have a cube that gets more opaque the closer you get to it.
If you'd like to adjust the maximum and minimum distances that Slender's opacity will interpolate between, you can do so by changing the respectively marked values in Slender's inspector.
As for how and why this works:
the first line inside the Update method in the script creates a variables called alpha and initializes it using Unity's InverseLerp method. InverseLerp takes three numbers 'from', 'to' and 'value'. It finds where between 'from' and 'to' 'value' is and returns this in the form of a fraction (a value between zero and one).
The second line sees the Color of the GameObject be set to a new Color. This new Color copies all the properties of the original color, except for the alpha value (transparency), which is set to the alpha variable that was created on the line above. Remember, each property of a Unity Color (i.e. red, green, blue and alpha) range between zero and one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131016im_/https://answers.unity.com/themes/thub/images/avi.jpg)