How to make Moving Platform -2D or 3D Unity - Tutorial

 

 How to make Moving Platform - Unity



How to make Falling Platform - Unity

How to make Moving Platform 2D or 3D in Unity by C# code easily Step by Step :

  • create a new gameObject , name it " Moving Platform " .


  • Inside the (Moving Platform)  add inside your Platform gameobject .

  • Add two gameObjects : Pos 1 , Pos 2 .
  • Create a new C# script and add it to (Platform) gameObject , copy and past this code :



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class  MovingPlatform : MonoBehaviour
{
    public static MovingPlatform instance;

    public Transform pos1, pos2;
    public float speed;
    public Transform startPos;
    Vector3 nextPos;

    // Start is called before the first frame update
    void Start()
    {
        instance = this;
        nextPos = startPos.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (transform.position == pos1.position)
        {
            nextPos = pos2.position;
        }
        if (transform.position == pos2.position)
        {
            nextPos = pos1.position;
        }
        transform.position = Vector3.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
    }
    private void OnDrawGizmos()
    {
        Gizmos.DrawLine(pos1.position, pos2.position);
    }
}


  • full the details like this :




  • the gameObjects : Platform , Pos 1 , Pos 2 :


unity 2d moving platform tutorial moving platform unity 2d how to make player stick to moving platform unity unity character controller moving platform rigidbody moving platform kinematic moving platform unity unity moving platform player not moving  how to make moving platform in unity 3d unity 2d moving platform tutorial moving platform unity 2d rigidbody moving platform kinematic moving platform unity unity moving platform player not moving