Advertisement
01010111

[Unity2D] Simple one way platforms

Nov 15th, 2013
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. /*
  4.  * Some simple code for One Way (cloud) Platforms.
  5.  * USAGE: works with horizontal platforms that only the player will stand on.
  6.  * Platform pivot should be where the player can stand. Player pivot should be at the player's feet.
  7.  * <3 - @x01010111
  8.  */
  9.  
  10. public class OneWayPlatform : MonoBehaviour {
  11.    
  12.     public string playerName = "Player";
  13.     private GameObject player;
  14.  
  15.     //Find player by name
  16.     void Start () {
  17.         player = GameObject.Find(playerName);
  18.         if (player == null) Debug.LogError("(One Way Platform) Please enter correct player name in Inspector for: " + gameObject.name);
  19.     }
  20.  
  21.     //Check to see if player is under the platform. Collide only if the player is above the platform.
  22.     void FixedUpdate () {
  23.         if (player != null) {
  24.             if (player.transform.position.y < this.transform.position.y) gameObject.collider2D.enabled = false;
  25.             else gameObject.collider2D.enabled = true;
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement