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
1
Question by OmegaVemon · Nov 23, 2010 at 12:59 AM · vector3distance

Vector3.Distance not working properly?

So I have an object that will change color when the mouse is over it. But only if the player is close enough.

The object's OnMouseEnter function is:

function OnMouseEnter () { if (Vector3.Distance(player.position,transform.position) <= maxBlockDistance) { renderer.material.color = overColor; } }

player is the Transform component of.. the player. maxBlockDistance is set to 5.0.

Some items work properly, but others won't have the color even though I am just next to them.

Comment
Add comment · Show 3
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 OmegaVemon · Nov 23, 2010 at 01:41 AM 0
Share

$$anonymous$$ore info:

It seems like only the items that are close to the "original" spawn point of the player react correctly. All other items won't, even though I move the player to them.

avatar image Eric5h5 · Nov 23, 2010 at 02:29 AM 0
Share

Vector3.Distance always works properly. It's really simple math that's impossible to get wrong, so you can rule that out.

avatar image Jessy · Nov 23, 2010 at 11:13 PM 0
Share

Did you make sure that the shaders you're using on all of the objects have a property called _Color?

3 Replies

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

Answer by OmegaVemon · Nov 23, 2010 at 11:58 PM

I would like to direct all of you to this question: http://answers.unity3d.com/questions/28547/transform-position-not-changing

It seems Vector3.Distance was not the problem, it was the fact that my first person controller's position never changes, even though I freely move it around. So that's the source problem.

Thanks guys.

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
avatar image
1

Answer by duck · Nov 23, 2010 at 08:29 AM

Your function won't ever get called if the mouse is "next to" the object, even if the distance is less than your "maxBlockDist" variable, because you have placed it in the "OnMouseEnter" function, so the mouse has to actually be over the object before the code can run at all. When the mouse is only next to the object, the code isn't even doing that distance check.

If you want to run some code when the mouse is near an object, you'll need to compare the object's position on-screen to the mouse's screen coordinates every frame - something like this:

var proximityRequired = 50 var overColor : Color; var nearColor : Color; var withinRange = false;

function Update() {

 var screenPos = camera.WorldToScreenPoint(transform.position);
 var mousePos = Input.mousePosition;
 var proximity = (screenPos-mousePos).magnitude;

 if (proximity &lt; proximityRequired) {
     renderer.material.color = nearColor;
     if (!withinRange) {
         withinRange = true;
         FunctionToCallWhenRangeEntered();
     }
 } else {
     renderer.material.color = normalColor;
     withinRange = true;
 }

}

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
avatar image
0

Answer by denewbie · Nov 23, 2010 at 01:31 AM

It might have happened because some or the objects ou attached the script to does not have a collider. Another eeason might be because your collider was not hit because it was too small or cover by some other collider. Attaching this scrit to the player also does not work.

Comment
Add comment · 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 OmegaVemon · Nov 23, 2010 at 01:51 AM 0
Share

All the objects seem to have a collider. I don't think the problem was due to the colliders though.

avatar image denewbie · Nov 23, 2010 at 07:11 AM 0
Share

In that case could it be because there are multiple layers of materials in some of your objects?

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

No one has followed this question yet.

Related Questions

Move an object to a specific distance 0 Answers

Vector3.Distance error plizzzzzz help 2 Answers

Vector3(x,y,variable)? 0 Answers

What am I doing wrong in my distance calculation? 0 Answers

distance to variable object 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