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 /
avatar image
0
Question by kevinseligmann · Oct 03, 2011 at 08:55 AM · javascriptmovementfunctioncollide

Problem accessing variable in javascript

Hi everyone, I'm trying to make a sphere (testObject1) change direction when collides with a rigidbody.

The idea is to keep the sphere bouncing from 1 point to another (testObject2 and testObject3). So, I came up with this:


 var speed = 0.10;
 var dropPoint = 1;
 
 function Update () {
     
     if(dropPoint == 1){
         transform.Translate(0, 0, speed);
     }else{
         transform.Translate(0, 0, speed - (speed * 2));
     }    
 }
 
 function OnCollisionEnter(collision : Collision) {
     for (var contact : ContactPoint in collision.contacts) {
     
         if(collision.collider == "testObject3"){
             dropPoint = 2;
         }else{
             if(collision.collider == "testObject2"){
                 dropPoint = 1;
             }
         }
         if(collision.collider != "testFloor"){
             print(collision.collider);
         }
     }
 }


The problem is that when the Sphere collides with testObject3, it just keeps going in the same direction and not in the opposite one as it should go.

What happening here? Any idea?

Thanks!

Comment
Add comment · Show 1
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 Lav-patel · Oct 03, 2011 at 12:23 PM 0
Share

u need reset speed ...

1 Reply

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

Answer by SilverTabby · Oct 03, 2011 at 11:21 AM

The majority of your problems are in this function:

 function OnCollisionEnter(collision : Collision) {
 for (var contact : ContactPoint in collision.contacts) {
 
         if(collision.collider == "testObject3"){
          dropPoint = 2;
         }else{
          if(collision.collider == "testObject2"){
           dropPoint = 1;
          }
         }
         if(collision.collider != "testFloor"){
          print(collision.collider);
         }
     }

I'm going to go line by line and explain what's going on here:

  • function ...{

  • This line is done perfectly. Moving on...

  • foreach( ContactPoint ) {

  • Huh? Why are you trying to loop through the points? You never use any of them! This loop does nothing. You only need to evaluate the body once

  • if(collider == "testObject3")

  • This will never evaluate to true. Why? Because you're comparing a collider with a string. What you're trying to do is

  • if(collider.**name** == "testObject3")

  • Now you're comparing a string with a string. This can and will evaluate to true.

  • There are several repeats of the above problem

  • if(collision.collider != "testFloor")

  • Again, this will never evaluate to false. If you always want this to print, then just remove the if in front of print altogether.

The only other thing I can add is that I would change your Update function to this, it looks more professional, and is easier to edit:

PLEASE DO NOT COPY-PASTE THIS! RETYPE IT BY HAND. It seems tedious, but trust me, it helps. You don't need to retype the comments, they exist so that you understand what you are typing.

 //this function is called once per frame.
 function Update ()
 {
     //switch-case is shorthand for if(dropPoint==1){}else if(==2){}else if(=="etc"){}else
     switch(dropPoint)
     {
         //if you forget the break, it will do case 1, then do case two, then do case 3...
         case 1:
             transform.Translate(0, 0,  speed * Time.deltaTime); break; 
         case 2:
             transform.Translate(0, 0, -speed * Time.deltaTime); break;
         default: /*if none of the above cases are true, do nothing*/ break;
     }
     //you can have more than one action per case, as long as there is a break; at the end
 }
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 kevinseligmann · Oct 04, 2011 at 07:09 AM 0
Share

Thank you very much SilverTabby, I can now see my mistakes and start to cleaning them up. As soon as I'm able to do the changes I'll let you know about it.

One other question... is it possible to edit the code within Unity? Because, I'm not able to edit the code in the right panel, only allowed to double click it, and it opens in Notepad. Is there any way to change this? Because it's kind of annoying to use an external editor, an external window just to make a simple change.

Once again, thanks!

avatar image syclamoth · Oct 04, 2011 at 07:39 AM 0
Share

Well, if you set it up right it should be using $$anonymous$$onoDevelop (which, if you do a lot of coding, you should have up at all times) which is a proper IDE (much better than notepad!)

avatar image kevinseligmann · Oct 04, 2011 at 07:45 AM 0
Share

Yeah, but isn't there a way to edit in-Unity the code? I mean, to edit something little, is there the possibility to edit from the Inspector itself when you select a javascript file?

avatar image syclamoth · Oct 04, 2011 at 07:52 AM 0
Share

Unity really isn't about editing assets in the editor- this applies to code just as much as it does to mesh models. Just like any other asset, script files must be edited from an external application!

avatar image kevinseligmann · Oct 04, 2011 at 08:00 AM 0
Share

In that case, thanks very much for taking the time to explain that to me! Thanks very much!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Javascript Movement Script. Why am I getting these errors? 2 Answers

Call JavaScript from c# Help! 0 Answers

Static Typing for JS function? 2 Answers

Doing something mutiple times in function() Start 1 Answer

Talking Code 2 Answers


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