- Home /
object distance
I want when the player reach an object (the distance between them may be 20 ) print "something" and then after key "e" pressed play animation
I know how to do it with key press , but I want that, the animation only plays when the distance between the player and the object is 20
Answer by sovalopivia · Jul 29, 2010 at 08:10 PM
You can use
if ( ( Vector3.Distance(thePlayer.transform.position, theObject.transform.position) <= 20.0f ) && Input.GetKeyDown("E") )
{
PlayAnimation();
}
http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html
assu$$anonymous$$g you have access to the player and the object in this script of course :)
You could save on a square root by using distance squared with if((thePlayer.transform.position - theObject.transform.position).sqr$$anonymous$$agnitude
Your answer
![](https://koobas.hobune.stream/wayback/20220613070041im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[2D] Object at X distance to Y direction from point 1 Answer
How can I get the distance between two objects? 3 Answers
Calculating distance, just with position? 1 Answer
how can i find the closest object (with the tag Point) 1 Answer
Sigh... Need help with moving object a distance over time 1 Answer