using System.Collections; using System.Collections.Generic; using UnityEngine; public class MovingPlatform : MonoBehaviour { /* You will need to add the following to the moving block object: * Rigidbody 2d: freeze the y axis and freeze rotation in the z axis, make it Kinematic * boxcollider 2d: make the size of the platform * boxcollider 2d: a second box collider that is very thin on top of the platform and isTrigger * (this detects collision with the player so that the method in player input works) * This script: change the inspector values to work for your scene, left and right bounds and speed, * you will need to drag the rigidbody into the inspector to move the block. */ public Rigidbody2D rb; //Left boundary of platform movement public float LeftBound = 0; //right boundary of platform movement public float RightBound = 1; //bool that controls movement left or right public bool movingRight = true; //speed of platform, 5 is fast, 1 is very slow public float platSpeed = 5; //movement value that is used to move the object public Vector2 movement; void Update() { //updates the speed and direction of the platform movement.x = platSpeed * Time.fixedDeltaTime; //ensures the platform does not move in the y direction movement.y = 0; } // Update is called once per frame void FixedUpdate() { //If statements detect where the platform is and switches direction if beyond the boundary if (gameObject.transform.position.x > RightBound) { movingRight = false; } else if (gameObject.transform.position.x < LeftBound) { movingRight = true; } if (gameObject.transform.position.y < 0) { // rb.movePosition.y = 0; } //these if statements apply the movement to the platform if (!movingRight) { Vector2 moveTo = (Vector2)transform.position - movement; transform.position = moveTo; } else { Vector2 moveTo = (Vector2)transform.position + movement; transform.position = moveTo; } } }