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 /
avatar image
0
Question by Psilocybe · Jan 19, 2016 at 03:40 PM · 2dscripting problem

How to set an extra condition (2D movement)

Hello,

I'm working on a 2D project. Everything is fine apart from one thing:

I have a ball that must land in a bucket for you to win the level. The ball is capable of bouncing out of the bucket, which would not count as a win. It must stay stationary inside it for you to win.

I made a script that tells the game that the level has been passed, 2 seconds after colliding with the bucket. The problem is, if the ball lands in the bucket and bounces out, the game obviously thinks you won. How do I add to the script so that the win condition must be a combination of hitting the bucket's collider AND being stationary INSIDE it for a set number of seconds?

Java or C# will do.

Thanks in advance.

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

2 Replies

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

Answer by skillbow · Jan 19, 2016 at 06:51 PM

Maybe do it something like this:

If there is a collision with the bucket, then set a flag to true. In FixedUpdate check to see if the flag is set to true and that the gameObject has stopped moving. If it has then the ball has stopped and stayed inside the bucket, else we assume that it's still moving. OnCollisionExit2D will let you know if the ball has left the bucket. This way there is no need to have any kind of timer for staying in the bucket.

I've not tested this code so may need some minor fixes. _stopVelocity can be set to anything you like but if it's set to 0.0f it may take quite a while to full stop.

 #pragma strict
 var _insideBucket = false;
 var _stopVelocity = 0.05f; 
 
  function OnCollisionEnter2D(other : Collision2D) 
  {
      
      if(other.gameObject.tag =="bucket")  {
  
              _insideBucket = true;
          }
 }
 
 function OnCollisionExit2D(other : Collision2D) {
     _insideBucket = false;
 
      Debug.Log("Bounced out!");
      Application.LoadLevel("restartMenu");
 }
 
 function FixedUpdate () { 
 
    if(_insideBucket) {
      var curVelocity = gameObject.GetComponent.<RigidBody2D>().Velocity.sqrMagnitude;
      if(curVelocity <= _stopVelocity) {
          Debug.Log("SavedLevel = 1");
          Application.LoadLevel("menu");
     }
 }
    



Comment
Add comment · Show 4 · 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 Psilocybe · Jan 19, 2016 at 07:16 PM 0
Share

Thank you so much!

I'm getting one error with the code, which is on line 24:

"The name 'RigidBody2D' does not denote a valid type ('not found')...

avatar image skillbow Psilocybe · Jan 19, 2016 at 07:18 PM 1
Share

Sorry should be Rigidbody2D (lowercase B). Code assumes of course that there is a rigidbody2D attached to the ball.

avatar image Psilocybe · Jan 19, 2016 at 07:37 PM 0
Share

Seriously, thanks a mil! It all works perfectly now.

I had to add 1 line of code to the OnCollisionExit function, to make sure it only applied to the bucket and not the obstacles & floors etc in the scene, but I neglected to mention that there were any obstacles so you weren't to know...

Cheers!

avatar image skillbow Psilocybe · Jan 19, 2016 at 07:47 PM 0
Share

Really pleased it worked. That's a good point on the collision exit check. All the best with your project.

avatar image
1

Answer by SoulGameStudio · Jan 19, 2016 at 04:33 PM

I guess one solution would be to start an Invoke when the ball lands in the bucket :

 Invoke("checkVictory", 2);

And then in the checkVictory function you just say:

  • If the ball is still in my bucket, (I guess you have already colliders than can tell you that) you win.

  • If the ball is not colliding with the bottom of the bucket anymore, you do nothing or lose or whatever you want.

Is that a solution for you?

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 Psilocybe · Jan 19, 2016 at 05:20 PM 0
Share

Thanks very much for your reply, that does seem to be what I need. $$anonymous$$y script is in JS, and this is what I have so far. It doesn't work though. Can you see where I've gone wrong?

 #pragma strict
 
 function OnCollisionEnter2D(other : Collision2D) 
 {
     
     if(other.gameObject.tag =="bucket") 
         {
 
             checkVictory();
             
             
         }
         }
         
 function checkVictory(){
 
     yield WaitForSeconds(2.0);
     if (Collision2D == true)
     {
         PlayerPrefs.SetInt("SavedLevel1", 1);
         Debug.Log("SavedLevel = 1");
         Application.LoadLevel("menu");
     }
     else if (Collision2D == false)
     {
         Debug.Log("Bounced out!");
         Application.LoadLevel("restart$$anonymous$$enu");
     }
 }

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

58 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

Related Questions

Trouble with GUI Button Level switching. 0 Answers

How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers

Jitter when moving around a circular path 0 Answers

Assets/Scripts/GameControll.cs(21,15): error CS0118: `UnityEngine.Object.Destroy(UnityEngine.Object, float)' is a `method' but a `type' was expected 0 Answers

Delete a specific GameObject (with tag) on a Specific Vector2 Location 0 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