• 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
Question by striker4000 · Jan 21, 2014 at 10:43 AM · rangeopacityslender

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

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 2 · 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 striker4000 · Jan 21, 2014 at 10:19 PM 0
Share

Thank You SOOOOOO much man!

avatar image brycedaawg · Jan 23, 2014 at 02:54 PM 0
Share

You're welcome

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

Slender type game 1 Answer

Multi-Dimensional array out of range issues. 1 Answer

Argument out of range 1 Answer

Random Teleportation using arrays! (Re-Spawn) 1 Answer

how do i tell if a object with a diffrent layer then mine comes within range 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