Endless Runner platform Heigh generator not working.
So I have a script to make my platforms spawn in different heights since it was spawning at places that were out of the camera then you had to add a function that would limit the heights within a Max and Min height, to keep it within camera range. My problem is, every time I set the function, when I take "//" off (which means I'm making it active, take place) then the function stops working and I have no idea why.
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformGenerator : MonoBehaviour
{
public GameObject Wood;
public Transform GenerationPoint;
public float DistanceBetween;
private float PlatformWidth;
private float MinHeight;
public Transform MaxHeightPoint;
private float MaxHeight;
public float MaxHeightChange;
private float HeightChange;
void Start()
{
PlatformWidth = Wood.GetComponent<BoxCollider2D>().size.x;
MinHeight = transform.position.y;
MaxHeight = MaxHeightPoint.position.y;
}
void Update()
{
if(transform.position.x < GenerationPoint.position.x)
{
HeightChange = transform.position.y + Random.Range(MaxHeightChange, -MaxHeightChange);
//if (HeightChange > MaxHeight)
{
//HeightChange = MaxHeight;
}
//else if (HeightChange < MinHeight)
{
//HeightChange = MinHeight;
}
transform.position = new Vector3(transform.position.x + PlatformWidth + DistanceBetween, HeightChange, transform.position.z);
Instantiate(Wood, transform.position, transform.rotation);
}
}
}
So, here is my script. If you notice in the update section, at the bottom, right after HeightChange = transform.position.y + Random.Range(MaxHeightChange, -MaxHeightChange);
I have the rules to make the spawning be within a certain range. I'll say, I am following a video to get make this possible and I am doing everything the video says but it's been giving me this problem so I'm taking it to Unity itself.
Thanks, peace
Im having the same issue
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Kill : MonoBehaviour
{
public GameObject player;
public GameObject springPrefab;
public GameObject platformPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.name.StartsWith("Platform"))
{
if (Random.Range(1, 7) == 1){
Destroy(collision.gameObject);
Instantiate(springPrefab, new Vector2(Random.Range(-5.5f,5.5f), player.transform.position.y + (14 + Random.Range(0.2f, 1f))), Quaternion.identity);
}else {
collision.gameObject.transform.position = new Vector2(Random.Range(-5.5f,5.5f), player.transform.position.y + (14 + Random.Range(0.5f, 1f)));
}
}else if(collision.gameObject.name.StartsWith("Spring"))
{
if (Random.Range(1, 7) == 1){
collision.gameObject.transform.position = new Vector2(Random.Range(-5.5f,5.5f), player.transform.position.y + (14 + Random.Range(0.2f, 1f)));
}else {
Destroy(collision.gameObject);
Instantiate(platformPrefab, new Vector2(Random.Range(-5.5f,5.5f), player.transform.position.y + (14 + Random.Range(0.5f, 1f))), Quaternion.identity);
}
}
}
}
Character freezes after two jumps and platforms aren't being generated