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 jong · Feb 03, 2013 at 02:21 PM · transformpositionvector3distancework

Distance won't work

Help!!!

This script was supposed to assign a specific distance when clicking an npc to start a connversation. If "dist" is less than "fixdist", the character can click to the npc.

This code works fine in my other event, I copy pasted it from there.

Here is the code:

 private var trgtObjct : Transform;
 
 function Start(){
 
     trgtObjct = GameObject.FindGameObjectWithTag("Player").transform;
 }
 
 function npcTalk(){
 
     var fixdist = 100.3872;
 
     if (trgtObjct!= null)
     {    
         var dist : float = Vector3.Distance(trgtObjct.position, this.transform.position);
     
         if (dist <= fixdist)
         {
             OnMouseDown();
         }
         else
             print("Lumapit ka");

}

sorry for the bad english.

Comment
Add comment · Show 8
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 Owen-Reynolds · Feb 03, 2013 at 03:50 PM 1
Share

How do you know the problem is distance? Anything could be causing the problem. $$anonymous$$aybe print dist right after you compute it, to check? It is printing "Lumapit ka" after each click?

avatar image jong · Feb 03, 2013 at 04:21 PM 0
Share

because the player can still click to the npc even if the distance is greater than the given variable(fixdist). I just copy pasted this from the other javascript file, which is also on the same scene as this. The code works fine on the other script. The print"Lumapit ka", doesn't affect the code, i put comment/remove that line from the script, still doesn't work

avatar image Wolfram · Feb 03, 2013 at 04:38 PM 0
Share

He was asking about "Lumapit ka" to see whether this code segment was ever executed, not because it could cause a bug.

avatar image jong · Feb 03, 2013 at 04:51 PM 0
Share

sorry, that code segment didn't appear so i removed it from the script.

avatar image Wolfram · Feb 03, 2013 at 04:58 PM 0
Share

But it was a good debug message ;-) If it was never printed, it means that either dist is never > fixdist - or that there was no object "Player" found, or that npcTalk() is never called. How/where do you call npcTalk()?

Also, a fixdist of 100.3872 sounds rather large. Are your scene dimensions really this big? Are your models in centimeters?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wolfram · Feb 03, 2013 at 04:40 PM

If the distance is smaller, you are explicitly calling OnMouseDown(). However OnMouseDown() is being called unconditionally (well, the condition is that the mouse is over the object's Collider when clicking) by the game engine on mouse click anyway, so your approach does not work this way.

Use your own function to place your code in that should be executed when you have a "valid" click, and don't call it "OnMouseDown".

Comment
Add comment · Show 3 · 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 jong · Feb 03, 2013 at 04:53 PM 0
Share

So I'll just have to put it on different function and not name it as "On$$anonymous$$ouseDown". Sorry I'm still new to javascript and unity, what do you mean by "valid" click??

avatar image Wolfram · Feb 03, 2013 at 04:56 PM 0
Share

Yes, that's what I meant. With valid I meant the conditional click you are trying to implement, where dist

avatar image jong · Feb 03, 2013 at 05:23 PM 0
Share

I'm currently working on it.

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

11 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

Related Questions

distance to variable object 1 Answer

how do i fix my script in the content below? 0 Answers

Reposition to minimum distance from target 2 Answers

How do you get the vector3 of one object and assign that vector3 to another object? 1 Answer

How to smooth movement object in one-axis? 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