Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Nov 24, 2017 at 03:02 AM by NEGATIVERAGDOLL.
avatar image
-1
Question by NEGATIVERAGDOLL · Nov 21, 2017 at 09:57 AM · javascriptplayerscript.distance

Calculate distance between player and object

Hello, I made a script to pick items up. I'm wondering if anyone can help me add some sort pf thing so that the script can detect how far away the player is. Basically so you can't pick the items up anywhere, and you have to be close to them. Thanks in advanced. #pragma strict

var item : GameObject;

function Start () { gameObject.transform.localRotation = Quaternion.Euler(0,0,0); }

function Update () { if(Input.GetKey(KeyCode.E)) { Drop(); } }

function Drop() { //Destroy the object that has this script attached Destroy(gameObject); }

Comment
Add comment
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

  • Sort: 
avatar image
0
Best Answer

Answer by phaninder15 · Nov 21, 2017 at 10:08 AM

You can calculate distance from object using Vector3.Distance(object.position,player.position);

 float minDist=2;
 float dist = Vector3.Distance(other.position, transform.position);
 if(dist < minDist)
 {
     pickup();
 }
 





Comment
Add comment · Show 5 · 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 NEGATIVERAGDOLL · Nov 21, 2017 at 10:27 AM 0
Share

Hello, thank you for the reply! how would I add this into my script? I'm new to scripting.

avatar image phaninder15 NEGATIVERAGDOLL · Nov 21, 2017 at 11:12 AM 0
Share

When you want to pick up, for example if you want to pickup when player press Button E

 void Update()
 {
       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
        {
                    float dist = Vector3.Dist(object.positon,player.position);
                    if(dist<$$anonymous$$Dist)
                    {    
                         pickup();
                     }
          }
 }



avatar image NEGATIVERAGDOLL phaninder15 · Nov 21, 2017 at 11:59 AM 0
Share

I've added it into the script($$anonymous$$ay be incorrectly placed) and I keep getting two errors saying to add semicolons but when I do the error doesn't go. Here's the script so far #pragma strict

var item : GameObject;

function Start () { gameObject.transform.localRotation = Quaternion.Euler(0,0,0); }

void Update() { if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)); { float $$anonymous$$Dist=2; float dist = Vector3.Distance(other.position, transform.position); if(dist < $$anonymous$$Dist) { pickup(); }

    Drop();
   }

}

function Drop() { //Destroy the object that has this script attached Destroy(gameObject); }

Show more comments

Follow this Question

Answers Answers and Comments

170 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 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 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 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 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 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 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 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 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

How to organise game data for multiplayer with a player manager? 0 Answers

Player respawn at death position 0 Answers

Need help to put together 2 scripts 0 Answers

Health Script 2 Answers

Need support on my basic navmesh ai script to follow the player at a certain distance 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