- Home /
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.
$$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.
Vector3.Distance always works properly. It's really simple math that's impossible to get wrong, so you can rule that out.
Did you make sure that the shaders you're using on all of the objects have a property called _Color?
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.
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 < proximityRequired) {
renderer.material.color = nearColor;
if (!withinRange) {
withinRange = true;
FunctionToCallWhenRangeEntered();
}
} else {
renderer.material.color = normalColor;
withinRange = true;
}
}
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.
All the objects seem to have a collider. I don't think the problem was due to the colliders though.
In that case could it be because there are multiple layers of materials in some of your objects?
Your answer
Follow this Question
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