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.
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");
}
}
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')...
Sorry should be Rigidbody2D (lowercase B). Code assumes of course that there is a rigidbody2D attached to the ball.
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!
Really pleased it worked. That's a good point on the collision exit check. All the best with your project.
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?
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");
}
}