Wednesday, July 2, 2014

One Way Platform : Unity Tutorial

Hi friends! Here is the tutorial for making One Way platforms in unity. I've got a very simple technique to do this job so that no one gets stuck at this problem.

Here is what you have to do.

Make a platform and add a box collider to it.

Create a Gameobject (it is used for collision checking), as a child to the platform,name it as colcheck, and add a box collider to it just below the box collider of the platform. Make sure that "is trigger" checkbox is on so that we can check if the character or any kind of player is in contact with it or not. It should look something like this.






Go to tags and layer an create a new tag 'platform' and assign this to all the colcheck game objects.
Now we will write a script and add it to the colcheck game object we just created. Here it goes.

function Start () {

var colliderofplatform : BoxCollider2D;

//It is collider of main platform, assign it in inspector
 
var oneway : boolean;

//this variable will be made true when the players is just below the platform so that the Box collider of collision checking game object can be disabled that will allow the player to pass through the platform

}

function Update () {

if (oneway)
  colliderofplatform.enabled=false;


 if (!oneway)
  colliderofplatform.enabled=true; 


//Enabling or Disabling the platform's Box collider to allowing player to pass  
}



function OnTriggerStay2D(coll : Collider2D) {

if (coll.gameObject.tag == "platform")
   oneway = true;

 //Checking the collison of the collision checking gameobject we created for checking if the player is just below the platform and needs to ignore the collison with the platform}
 
}

function OnTriggerExit2D(coll: Collider2D) {
   oneway = false;


//It is done so that we can reactivate the collider of the platform for the player to stand on that platform
}


Now you will see that our one way collider is working the way it should. If there is some problem with it, add it to comments and I will fix it.

I'll upload the project file also. Keep reading, keep enjoying and playing with Unity, thats a nice engine. 

No comments:

Post a Comment