- Home /
Making a GUI texture fade in and out depending on player proximity with enemy
Hello Unity answers, unfortunately I have gotten completely stuck and decided to turn to the community for suggestions. Normally I'd try and find the answer myself but I thought I'd give this a try. Anyway, the issue I've been having is making my GUI texture fade in and out depending on the First Person Controllers proximity with the enemy, which so far is just a white cube for testing purposes. I found this piece of Javascript which I am currently using but seems it to be for more stationary objects rather than an object set to follow and eventually collide and kill the player. For example if the player remains motionless the enemy will simply collide with the player without fading beforehand and only fade in and out if the First person controller is moving. So simplify my point the fade needs to work regardless if the player is stationary or moving. This is the current code and it is attached to the enemy object.
var myGui: GUITexture;
var fadeSpeed : float = 1;
function OnTriggerEnter () {
myGui.active = true;
myGui.color.a = 0;
for (t = 0.0; t < fadeSpeed; t+= Time.deltaTime) {
myGui.color.a = Mathf.Lerp (0, 1, t / fadeSpeed);
yield;
}
}
function OnTriggerExit () {
for (t = 0.0; t < fadeSpeed; t+= Time.deltaTime) {
myGui.color.a = Mathf.Lerp (1, 0, t / fadeSpeed);
yield;
}
myGui.active = false;
}
how about you used the distance of the two objects ins$$anonymous$$d of having triggers? you can get the distance by subtracting their transfrom positions.
Answer by ScroodgeM · Jul 27, 2012 at 01:14 PM
using UnityEngine; using System.Collections; public class DistanceAlpha : MonoBehaviour { public Transform Target; public GUITexture MyGuiTexture; public float DistanceInvisible = 15f; public float DistanceOpaque = 10f; Transform _transform; void Start() { _transform = transform; } void Update() { float distanceSquared = (_transform.position - Target.position).sqrMagnitude; Color c = MyGuiTexture.color; c.a = (distanceSquared > DistanceInvisible * DistanceInvisible) ? 0f : (distanceSquared < DistanceOpaque * DistanceOpaque) ? 1f : (DistanceInvisible - Mathf.Sqrt(distanceSquared)) / (DistanceInvisible - DistanceOpaque); MyGuiTexture.color = c; } }
Answer by tlews · Apr 26, 2013 at 03:57 PM
How did you define the trigger for this code? I've put this script on the player but I have no idea how to define a specific collision box?
Your answer
Follow this Question
Related Questions
Repeat fade in and out 1 Answer
How to make a GUITexture fade In? 1 Answer
GUI.DrawTexture over screen 2 Answers
Gui appear and then fade to black 1 Answer
FadeIn/FadeOut GUI Menu and Text 1 Answer