# 2D Platform Movement [Solved]

Hello guys,

I’m hoping someone can look over my logic here and give me a hand. I’m trying to make a platform move back and forth. I believe I’ve set it up correctly so all I have to do is attach it to an object, set the values and let it go.

This is what I’ve got so far, and i’m not too sure why its not moving, could be I’ve stared at my code too long…

Anyhow;

``````using UnityEngine;
using System.Collections;

public class MovePlatform : MonoBehaviour {

public bool horizontalPlatform = true;
public int movementSpeed = 5;
public int xMin = 5;
public int xMax = 5;
public int yMin = 5;
public int yMax = 5;

private Vector2 startingPos;
private Vector2 runningPos;
private float currentPosX;
private float currentPosY;
private Vector2 minMaxCheck;

// Use this for initialization
void Awake () {

if(horizontalPlatform == true){
HorizontalMovement();
}else{
VerticalMovement();
}
}

// Update is called once per frame
void Update () {

if(horizontalPlatform == true){
runningPos = transform.position;

if(runningPos.x == startingPos.x){
transform.Translate(Vector2.right * movementSpeed * Time.deltaTime);
}else if(runningPos.x >= minMaxCheck.y){
transform.Translate(Vector2.left * movementSpeed * Time.deltaTime);
}else if(runningPos.x <= minMaxCheck.x){
transform.Translate(Vector2.right * movementSpeed * Time.deltaTime);
}

}else{

}

}

void HorizontalMovement(){

startingPos = transform.position;        //Get the current postion
currentPosX = startingPos.x;            //Get the x value for horizontal movement
//print(currentPosX + " - currontPosX");

minMaxCheck.x = currentPosX - xMin;     //This is the min X value or Left Check
minMaxCheck.y = currentPosX + xMax;     //This is the max X vakue or Right Check

//print("Left Check: " + minMaxCheck.x);
//print("Right Check: " + minMaxCheck.y);
}

void VerticalMovement(){

startingPos = transform.position;
currentPosY = startingPos.y;
//print(currentPosY + " - currontPosY");
}
}
``````

My other thing is, I’m not sure how to move the play in the same direction as the platform with out falling off. Any advice on that would be appreciated.

Thanks for the help in advanced!

Because you want something to move back and forth in a loop, this is called a Wave. Use the wave function to make something back and forth based on t time passed. Use Mathf.Sin or Mathf.Cos to get the back and forth working.

Try this, this should get your Transform oscillating.
Oscillator.cs

``````using UnityEngine;
using System.Collections;

public class Oscillator : MonoBehaviour {
[SerializeField] float amplitude = 1.0f;
[SerializeField] float speed = 1.0f;
[SerializeField] Axis axis = Axis.XAxis;

Vector3 origin = Vector3.zero;
float t = 0.0f;

void Start() {
this.origin = this.transform.position;
}

void Update() {
t = Time.realtimeSinceStartup;

Vector3 position = this.transform.position;

switch(this.axis) {
case Axis.XAxis: {

position.x = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.x;

this.transform.position = position;

} break;
case Axis.YAxis: {

position.y = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.y;

this.transform.position = position;

} break;
case Axis.ZAxis: {
position.z = this.amplitude * Mathf.Sin(Time.realtimeSinceStartup * this.speed) + this.origin.z;

this.transform.position = position;

}break;
}
}
}

public enum Axis {
XAxis,
YAxis,
ZAxis
}
``````

Thanks mate, appreciate the insight and that script worked out very well!
Now onto getting the player to stay on the platform.

HoldObject.cs

``````using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Collider))]
// [RequireComponent(typeof(Collider2D))] // Use this for 2D
public class HoldObject : MonoBehaviour {
void OnTriggerEnter(Collider coll) {
coll.transform.parent = this.transform;
}

void OnTriggerExit(Collider coll) {
coll.transform.parent = null;
}

/*
void OnTriggerEnter2D(Collider2D coll) {
coll.transform.SetParent(this.transform);
}

void OnTriggerExit2D(Collider2D coll) {
coll.transform.SetParent(null);
}*/
}
``````

HoldObject.cs goes on the platform correct? My player doesn’t move with it; I’m using Collider2D.
Thanks again for sharing this with me. Did you write these or use a reference somewhere?

Edit) I see OnTriggerEnter2D needs the Ridgedbody attached to it doesn’t it? When I do that, and hit my platform is starts spinning and taking parts of my environment with it rather than the player. Do I have this setup wrong? I can post a picture of my inspector if needed.

HoldObject.cs goes on the platform. You will need 2 colliders though. Add an extra Collider2D such as a BoxCollider2D and set as a trigger. Adjust the Trigger zone where you want the player to be “held” at. You do not need a rigidbody, if you add a rididbody it will spin if you have the Z Rotation unlocked.

Thanks Polymorphik! I got it working now, though it seems that the player doesn’t move evenly with the platform, a bit slower than what the platform moves at. However, when I drop one of my other objects on it, it works perfectly; in this case my medkit object.

If I may make a suggestion that worked great in my game:

I made an Parent with two children, one with the mesh, and the other was an offset empty point. I then rotated the empty point so that it made a corkscrew motion. Then I told the mesh to follow the x and y but ignore the z.

The result was a perfect signwave.

Thanks for the tip mate, I’ll try that out too. I still haven’t had any luck with the player’s position on the platform. It does become the child but it doesn’t move at the same speed. I’ll keep looking into that one.

Edit: I fixed the slow movement for the player on the platform. Turns out I had interpolate turn on. Thanks for the help everyone!