Unity2d - Moving platforms without character falling off. [C#]

While there are many guides about how to make moving platforms in Unity2d, most are confusing on how to make characters and objects on them to move along.

My implementation includes two empty game objects which will point the start and end point of the moving platform and the actual platform sprite. The platform we want to move horizontally, vertically and diagonally.

mov_plat.png

The public variables are the following shown above, which are self-explanatory.


using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour {
    [SerializeField]float movingSpeedx;
    [SerializeField]float movingSpeedy;
    public GameObject platform; //The actual sprite
    public GameObject start;
    public GameObject end;
    private bool orientation; //Where the platform is facing.
    private GameObject player;
    private bool horiz_movement; 
    private bool verti_movement;
    
    // Use this for initialization
    void Start () {
        player = GameObject.Find ("2DCharacter"); //Player's character.
        orientation = true;
                //Is it horizontal movement?
        if (end.transform.position.y !=  start.transform.position.y ) {
            horiz_movement = false;
        } else {
            horiz_movement = true;
        }
        //Is it vertical movement?
        if (end.transform.position.x != start.transform.position.x) {
            verti_movement = false;
        } else {
            verti_movement = true;
        }
        //Move the platform at the start point.
        platform.transform.Translate (start.transform.position.x, start.transform.position.y, start.transform.position.z);
    }
  
    void FixedUpdate () {       
        if (horiz_movement == true && verti_movement == false) {
            if (orientation) {
                platform.transform.Translate (Vector2.right * movingSpeedx * Time.deltaTime);
                if (end.transform.position.x < platform.transform.position.x) {
                    orientation = false;        
                }
            } else {
                platform.transform.Translate (-Vector2.right * movingSpeedx * Time.deltaTime);
                if (start.transform.position.x > platform.transform.position.x) {
                    orientation = true;     
                }
            }
        }else if(horiz_movement == false && verti_movement == true){
            if (orientation) {
                platform.transform.Translate (-Vector2.up * movingSpeedy * Time.deltaTime);
                if (end.transform.position.y > platform.transform.position.y) {
                    orientation = false;        
                }
            } else {
                platform.transform.Translate (Vector2.up * movingSpeedy * Time.deltaTime);
                if (start.transform.position.y < platform.transform.position.y) {
                    orientation = true;     
                }
            }
        }else if(horiz_movement == false && verti_movement == false){
            if(start.transform.position.x < end.transform.position.x && start.transform.position.y < end.transform.position.y){
                if (orientation) {
                    platform.transform.Translate (Vector2.up * movingSpeedy * Time.deltaTime);
                    platform.transform.Translate (Vector2.right * movingSpeedx * Time.deltaTime);
                    if (end.transform.position.y < platform.transform.position.y && end.transform.position.x < platform.transform.position.x) {
                        orientation = false;        
                    }
                } else {
                    platform.transform.Translate (-Vector2.right * movingSpeedx * Time.deltaTime);
                    platform.transform.Translate (-Vector2.up * movingSpeedy * Time.deltaTime);
                    if (start.transform.position.y > platform.transform.position.y && start.transform.position.x > platform.transform.position.x) {
                        orientation = true;     
                    }
                }
            }else{
                if (orientation) {
                    platform.transform.Translate (-Vector2.up * movingSpeedy * Time.deltaTime);
                    platform.transform.Translate (Vector2.right * movingSpeedx * Time.deltaTime);
                    if (end.transform.position.y > platform.transform.position.y && end.transform.position.x < platform.transform.position.x) {
                        orientation = false;        
                    }
                } else {
                    platform.transform.Translate (-Vector2.right * movingSpeedx * Time.deltaTime);
                    platform.transform.Translate (Vector2.up * movingSpeedy * Time.deltaTime);
                    if (start.transform.position.y < platform.transform.position.y && start.transform.position.x > platform.transform.position.x) {
                        orientation = true;     
                    }
                }
            }
            
        }
    }
    //If character collides with the platform, make it its child.
    void OnCollisionEnter2D(Collision2D coll){
        if (coll.gameObject.tag == "Player") {
            MakeChild ();   
        }
    }
        //Once it leaves the platform, become a normal object again.
    void OnCollisionExit2D(Collision2D coll){
        if (coll.gameObject.tag == "Player") {
            ReleaseChild(); 
        }
    }
    
    void MakeChild(){
        player.transform.parent = platform.transform;
    }
    
    void ReleaseChild(){
        player.transform.parent = null;
    }   
}

The platform begins from the start point and progress towards the end point’s X and Y but this is depending on the movement speed set. Minor adjustment is required when using it and remember that the start X point should always be equal or less than the X of end point. This can be easily modified.

There are some issues with this but since I am still learning C# and the Unity3D platform, better implementations should exist including tools I am not currently aware of.

 
114
Kudos
 
114
Kudos

Now read this

Unity2d - Fix the 2D basic character asset to avoid getting stuck on walls [C#]

The ability of the basic 2D character Robot from Sample Asset collection to get stuck onto walls, can be really annoying. One option would be to increase the friction of the objects, it interacts with but that is not an optimal solution... Continue →