- Home /
2D Platformer moving platform help C#
I'm currently trying to make a 2D platformer and need to make a vertical moving platform. However, the platform dosen't seem to move at all with my current code. Can anyone see why this isn't working?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundMovement : MonoBehaviour
{
private Transform startPos;
public Transform target;
public float speed;
private bool moveUp;
void Start()
{
startPos.position = transform.position;
moveUp = true;
}
void Update()
{
float step = speed * Time.deltaTime;
if (transform.position == target.position)
{
moveUp = false;
}
else if (transform.position == startPos.position)
{
moveUp = true;
}
if (moveUp == false)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
else if (moveUp)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
}
Answer by Xx_QuIcKScOpErZz_xX · Jun 28, 2017 at 08:53 PM
The startPos needs to be a Vector3, not a transform. and @FireStone720 is right with part of the logic of the program. A few tweaks and it works:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TheosAScrub : MonoBehaviour
{
private Vector3 startPos;
public Transform target;
public float speed;
private bool moveUp;
void Start()
{
startPos = transform.position;
moveUp = true;
}
void Update()
{
float step = speed * Time.deltaTime;
if (transform.position == target.position)
{
moveUp = false;
}
else if (transform.position == startPos)
{
moveUp = true;
}
if(moveUp == false)
{
transform.position = Vector3.MoveTowards (transform.position, startPos, step);
}
else if (moveUp)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
}
Answer by Trevdevs · Jun 28, 2017 at 06:57 PM
Found it, your setting in line 31 the destination to target pos which you don't want to do because moveUp is false you want to return to the startPos so change. Also if the problem continues make sure you set the speed value in the inspector i forget to do this a lot to :) It worked for me when I fixed it in my project hope it does for you to cheers !
if(moveUp == false)
{
transform.position = Vector3.MoveTowards (transform.position, startPos.position, step);
}
Thanks for the reply but unfortunately this did not work, but I've narrowed down the problem line 15 is not working, I.$$anonymous$$ moveUp is not initially being set to true. No idea why.
That is weird it is being set to true on start for me I copied your script exactly on a cube in my scene.
Try setting moveup to true in the inspector ins$$anonymous$$d of the code as well.
Setting the moveUp true/false in the inspector makes it move up but that's it. Also the startPos is not being set in the Start method, it appears to be null.
Answer by cstooch · Jun 28, 2017 at 08:45 PM
How do you know that moveUp isn't being set to true in the start? Do a Debug.Log after that line and check if it is reached.
Now, outside of that, you have set your "target" variable in the inspector, yes? What exactly is this being set to as well? It would need to be set to something that represents the platform's position at it's fully raised position (i.e. it shouldn't be the platform itself, but perhaps an empty game object located at the new position)
Your answer
Follow this Question
Related Questions
How to make character stop at wall? 1 Answer
How to make 3 bullets fire at different angles 1 Answer
Sprite image not changing 0 Answers
How to instantiate prefabs at random postions in 2d 1 Answer