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 Despot · Apr 02, 2013 at 05:53 AM · javascriptdistanceif statement

Javascript Distance Check Not Working

I'm working on a script which means that if a player presses the 'e' key when close to another player, they will attack. Although I can see in the inspector that the variable 'distance1' is working and changing as the characters move, I don't get any of my Debug.Log statements and pressing the e key does nothing. I'm guessing this means that the statement 'if(distance1 < 2)' is never fulfilled. Any ideas why this could be? Thanks.

 var distance1: float;
 
 function Update () {    
 
     var manager = GameObject.Find("NetworkManager");
     var script = manager.GetComponent(NetworkManagerScript);
     
     var Player1 = script.Player1;
     
     distance1= Vector3.Distance(gameObject.transform.position, Player1.transform.position);
     
     if(distance1 < 2){
     
         Debug.Log("Close");
     
         if(Input.GetKeyDown("e")){
         
             Debug.Log("Dead");
             Destroy(Player1);
         }
     }
 }
Comment
Add comment · Show 2
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 fafase · Apr 02, 2013 at 05:58 AM 0
Share

Here is a better version of yours:

 var distance1: float;
 var Player1:GameObject; 
 function Start() {
    var manager = GameObject.Find("Network$$anonymous$$anager");
    var script = manager.GetComponent(Network$$anonymous$$anagerScript);
    Player1 = script.Player1;
 } 
 function Update(){
    distance1= Vector3.Distance(gameObject.transform.position,Player1.transform.position);
  
    if(distance1 < 3){ 
       Debug.Log("Close"); 
       if(Input.Get$$anonymous$$eyDown("e")){  
         Debug.Log("Dead");
         Destroy(Player1);
       }
    }
 }

Also, try with a greater value than 2. See your player may be be already between 0.5 and 1 wide and same for the other player meaning that they would have to be touching each other (don't laugh will you) for the statement to work.

avatar image Eric5h5 · Apr 02, 2013 at 06:17 AM 0
Share

Also the "distance1" variable should be declared inside Update, unless you're using it in another function.

1 Reply

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

Answer by Despot · Apr 02, 2013 at 09:46 AM

Thanks for the responses, but for future reference, the issue was that for some reason it wasn't taking the distance from a network instantiated player. I fixed it by adding all the if statements into an 'if(networkView.isMine)' statement.

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

12 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

Related Questions

Spawn along local axis. 3 Answers

Finding Distances With Vector3 1 Answer

GUI Counter Based on Animation or RaycastHit 0 Answers

What is causing my AI script to interpret wrong data? 1 Answer

distance script not working!!! 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