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 $$anonymous$$ · Jul 15, 2014 at 07:59 AM · javascript

How to set distance?

(JAVASCRIPT) I want to make it so you have to turn off a transformer. So far I have this:

 #pragma strict
 
 function Start () {
         light.color = Color.cyan;
 }
 
 function OnMouseOver ()
 {
     if 
         if ( Input.GetMouseButtonDown(0))
 
         {  
            light.color = Color.red;
         }
     }


This script is of course incorporated with the light, not the transformer itself. (Check images for before and after)

Image and video hosting by TinyPic

           Notice how far I am. I want to make it so you stand, for instance, a yard away, at most.

Comment
Add comment · Show 6
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 gjf · Jul 15, 2014 at 08:36 AM 0
Share

please use code tags.

avatar image vlames · Jul 15, 2014 at 08:50 AM 0
Share

I don't really understand, do you want to only change the light when you're close to the object?

if so use if(Vector3.Distance(transform.position , player.transform.position){"your code"}

avatar image Fornoreason1000 · Jul 15, 2014 at 09:10 AM 0
Share

what do you mean by transformer? do you mean gameobject.transform or do you mean the step up transformers used in electrical distribution?

also please format your code... sometimes the editor does a really poor job of doing it the first time so it can be necessary to re-paste it.

so far all I know is you want something red and at a distance you want something to turn off..

avatar image $$anonymous$$ · Jul 15, 2014 at 09:48 AM 0
Share

Okay... Sorry for confusing you guys... @gjf I do not understand what you mean by code tags, I am new to this.

@vlames I want to change it when I click the left mouse button A$$anonymous$$A what I have, but I want it to change color, when I click it, WHILE close.

@Fornoreason1000 I do mean transformer to distribute electricity. That's part of my game, where you have to turn it off. Re-format as in the coding?

~~~ START SCRIPT ~~~

function Start () { light.color = Color.cyan; }

function On$$anonymous$$ouseOver () { if ( Input.Get$$anonymous$$ouseButtonDown(0))

     {  
        light.color = Color.red;
     }

}

~~~ END SCRIPT ~~~

Easier? I know what you mean, haha, it didn't paste in well.

avatar image Fornoreason1000 · Jul 15, 2014 at 10:08 AM 1
Share

Ok now we are on the same page...

This what i meant I edited you code... you said 1 yard... so i set it to work within exactly one yard

 function Start () { 
     
     light.color = Color.cyan; 
     
     }
     
     function On$$anonymous$$ouseOver () { 
 
      if (Input.Get$$anonymous$$ouseButtonDown(0) && (Vector3.Distance(transform.position , player.transform.position) < 0.9144f )           
     
         {  
            light.color = Color.red;
         }
     }


Show more comments

1 Reply

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

Answer by FirePlantGames · Jul 16, 2014 at 01:52 AM

 var player : Transform; 
 var minDistance : float;
 
 function Start () {
         light.color = Color.cyan;
 }
  
 function OnMouseOver ()
 {
     if ( Input.GetMouseButtonDown(0) && Vector3.Distance(transform.position, player.position) <= minDistance)
         {  
            light.color = Color.red;
         }
     }
 

that should work, let me know if it works!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving data to xml file (I have the loading down) 0 Answers

Customize a scripted GUI 1 Answer

adding multiple values to single function/variable. 0 Answers

UnityScript to C# conversion help? 1 Answer

Coordinates of GUITexture 0 Answers


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