E
E
Egorian2018-01-28 16:12:03
C++ / C#
Egorian, 2018-01-28 16:12:03

How to make moving platforms?

I'm making a 2D platformer. I made a platform with such a script

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

public class MonsterBehaviour : MonoBehaviour {

    public float speed = 3f;

    public Transform[] waypoints;
    private int ArrayIndex;

    void Start () {
  ArrayIndex = 0;
    }
    

    void Update () {
  

  transform.position = Vector2.MoveTowards (transform.position, waypoints[ArrayIndex].transform.position, speed * Time.deltaTime);
  CheckWayPoint ();
    }

    void CheckWayPoint(){
  int test = ArrayIndex + 1;

   if (Vector2.Distance(transform.position,waypoints[ArrayIndex].transform.position)<0.1f) {
   if (test > 1) {
    ArrayIndex = 0;
   } else {
    ArrayIndex++;
   }

    }
}
}

When the character stands on the platform and it moves down, the Persian, as it were, breaks away from it.
How to make it not come off the platform?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2018-01-28
@Egorian

It is necessary to make the player a child of the platform.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question