Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pattington_Bear · Jul 26, 2012 at 08:34 PM · guitextureplayerfadeproximity

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; 
 
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pheash · Jul 27, 2012 at 08:22 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
    }
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pattington_Bear · Jul 27, 2012 at 02:21 PM 0
Share

Works great!

avatar image ScroodgeM · Jul 27, 2012 at 02:25 PM 0
Share

accept answer then 8) good luck 8)

avatar image Pattington_Bear · Jul 27, 2012 at 02:31 PM 0
Share

Sure thing, Sorry multitasking...

avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges