Home > Software design >  Platform Positions Randomly Generated - Not Generating within possible range
Platform Positions Randomly Generated - Not Generating within possible range

Time:11-19

Basically, the issue I have is that the platforms generate vertical however they go off to the top right (See Image). Therefore the player can't make the jump to the platforms, without me having to increase their speed and jump values. I'd like to make it possible for the platforms to be generated at a random range, that is possible to jump too.

Platform Generation Code:

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

public class PlatformGenerator : MonoBehaviour
{
    public GameObject thePlatform;
    public Transform generationPoint;
    public float distanceBetween;

    private float platformWidth;


    public float distanceBetweenMin;
    public float distanceBetweenMax;

    // Start is called before the first frame update
    void Start()
    {
        platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.y < generationPoint.position.y)
        {
            distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);

            transform.position = new Vector3(transform.position.y   platformWidth   distanceBetween, transform.position.x, transform.position.z);

            Instantiate(thePlatform, transform.position, transform.rotation);
        }
    }
}

Platform Destroyer Code

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

public class PlatformDestroyer : MonoBehaviour
{
    public GameObject platformDestructionPoint;

    // Start is called before the first frame update
    void Start()
    {
        platformDestructionPoint = GameObject.Find("PlatformDestructionPoint");
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x < platformDestructionPoint.transform.position.x)
        {
            Destroy(gameObject);
        }
    }
}

Here is an image of what I'm experiencing. Image Detailing The Issue

CodePudding user response:

Looks like in

 transform.position = new Vector3(transform.position.y   platformWidth   distanceBetween, transform.position.x, transform.position.z);

you flipped the X and Y and it should probably rather be

 transform.position = new Vector3(transform.position.x   platformWidth   distanceBetween, transform.position.y, transform.position.z);

Which you can btw also simply write as

transform.position  = new Vector2(platformWidth   distanceBetween, 0);

or if you actually rather wanted them also to be vertical (like stairs) you probably rather meant something like e.g.

transform.position  = new Vector2(platformWidth, distanceBetween);
  • Related