• 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
-1
Question by Claymore · Jan 25, 2013 at 04:52 PM · raycastdistance

Why this script is not working?

Hi,i have script,i wanted to make it that i can only press "n" button from a certain distance,but its not working,why,please check it out and help me ;(

         var myDistance: float = 20;
         var cube : GameObject;
         var jointExist : boolean;
        
         function OnCollisionEnter(c : Collision) {
      
         if(!jointExist){
      
           var joint = gameObject.AddComponent(FixedJoint);
      
           joint.connectedBody = c.rigidbody;
      
           jointExist = true;
      
        }
      
     }
        
         function Update(){
             if(Input.GetKeyDown(KeyCode.N)){
                 if(Vector3.Distance(transform.position, cube.transform.position) < myDistance){
                     if(!jointExist){
                         var joint = gameObject.GetComponent(FixedJoint);
                         Destroy(joint);
                         jointExist = false;
                     }
                 }
             }
          
         }
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 Dave-Carlile · Jan 25, 2013 at 04:59 PM 0
Share

What exactly isn't working? Are you getting any errors in the console? If not, add Debug.Log messages right after Get$$anonymous$$eyDown to see what the variable values are...

 Debug.Log("Distance=" + Vector3.Distance(transform.position, cube.transform.position));
 Debug.Log("myDistance=" + myDistance);
 Debug.Log("joinExists=" + jointExists);

Do the values make sense? Are they what you expect? Do they satisfy the conditionals so the code inside the varous if statements executes?

avatar image Vonni · Jan 25, 2013 at 05:33 PM 0
Share

Is distance 20 in inspector as well? I've messed up thinking I got value in script many times.

1 Reply

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

Answer by Gizmoi · Jan 25, 2013 at 08:34 PM

I'm not 100% sure what this script is attempting to do, but I imagine that in Update, the 3rd if statement should read:

 if (jointExist == true)

rather than checking if it's false.

Comment
Add comment · Show 1 · 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 Claymore · Jan 25, 2013 at 09:28 PM 0
Share

Well it should work that when i press "n" the cube gets sticky and it stick to any collision.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Multiple Cars not working 1 Answer

Raycast Destroys player. 1 Answer

How To Invoke RayCastAll? 1 Answer

Selecting and deselecting not working 0 Answers

How to instantiate gameobject on raycast collision. 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