Hi, I am pretty new to Unity and I am trying to make a platform jumping game. I got this script working as it should from a Youtube video but I wanted to make the platform move randomly because I have multiple platforms that gonna spawn when I start the game. So basically when the platform spawn 1 should go from left to right and the other should go from right to left.
public Transform pos1, pos2;
public float speed;
public Transform startPos;
Vector2 nextPos;
// Start is called before the first frame update
void Start()
{
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 = Vector2.MoveTowards(transform.position, nextPos, speed * Time.deltaTime);
}
Since you give 2 points where the platform moves between just let the other start at the opposite side (so exchange the points). And I mean not in code but the “waypoint” gameobjects you drag into the inspector fields of each platform.
It’s preferable to have the same code run/react to different input data than to have to manage such stuff in code since that tends to get really complex fast and is also harder to debug/tune/balance.
Does that help?`If not you should specify more what exactly you need help with in your code.
I guess this is also a way to do it. But basically, I wanted to have the platform start from a random point from the 2 positions then they can go from left to right or right to left. For example, from pos1 = 1.5f and pos2 = -1.5f, I wanted the platform to start/spawn in a random range between these 2 pos and then move left to right or right to left.