TrackGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrackSegmentGenerator : MonoBehaviour
{
public GameObject[] straightSegments;
public GameObject[] ascendingSegments;
public GameObject[] descendingSegments;
public int numberOfSegments;
public Vector2 startPosition;
public float largeUpDownFrequency = 0.1f;
public float flatFrequency = 0.6f;
private void Start()
{
GenerateTrack();
}
private int lastSegementType = 0;
private void GenerateTrack()
{
Vector2 currentPosition = startPosition;
float distanceSinceLastEnemy = 0f;
for (int i = 0; i < numberOfSegments; i++)
{
GameObject newSegment;
float randomValue = Random.Range(0f, 1f);
List segmentTypes = new List
{
straightSegments,
ascendingSegments,
descendingSegments
};
// Introduce large ups and downs based on frequency
if (randomValue < largeUpDownFrequency)
{
// segmentTypes.Remove(straightSegments);
}
if (randomValue > flatFrequency) {
segmentTypes.Remove(descendingSegments);
segmentTypes.Remove(ascendingSegments);
}
int randomSegementType = Random.Range(0, segmentTypes.Count);
if (lastSegementType == 1 || lastSegementType == 2) {
randomSegementType = 0;
}
lastSegementType = randomSegementType;
// Choose a random segment type from the remaining options
GameObject[] chosenSegmentType = segmentTypes[randomSegementType];
//random straight version, random ascending version
int randomIndex = Random.Range(0, chosenSegmentType.Length);
newSegment = Instantiate(chosenSegmentType[randomIndex], currentPosition, Quaternion.identity);
// Set the end position and control points for the generated segment
TrackSegment trackSegment = newSegment.GetComponent();
currentPosition += trackSegment.endPosition;
// Check if a gap should be created
if (Random.value < trackSegment.gapProbability)
{
float gapSize = Random.Range(trackSegment.minGapSize, trackSegment.maxGapSize);
currentPosition += new Vector2(gapSize, 0f);
}
// Update the EdgeCollider2D and LineRenderer with the generated Bezier curve points
int curveResolution = 50;
Vector2[] edgeColliderPoints = new Vector2[curveResolution + 1];
for (int j = 0; j <= curveResolution; j++)
{
float t = j / (float)curveResolution;
Vector2 point = CalculateBezierPoint(t, Vector2.zero, trackSegment.controlPoint1, trackSegment.controlPoint2, trackSegment.endPosition);
edgeColliderPoints[j] = point;
// Set the LineRenderer position
trackSegment.lineRenderer.positionCount = curveResolution + 1;
trackSegment.lineRenderer.SetPosition(j, point);
}
trackSegment.edgeCollider.points = edgeColliderPoints;
// Spawn enemies based on spawn probability
if (Random.value < trackSegment.enemySpawnProbability)
{
distanceSinceLastEnemy += Vector2.Distance(currentPosition, currentPosition - trackSegment.endPosition);
if (distanceSinceLastEnemy >= Random.Range(trackSegment.minEnemySpawnDistance, trackSegment.maxEnemySpawnDistance))
{
float t = Random.Range(0f, 1f);
Vector2 spawnPosition = CalculateBezierPoint(t, Vector2.zero, trackSegment.controlPoint1, trackSegment.controlPoint2, trackSegment.endPosition);
spawnPosition += new Vector2(0f, trackSegment.enemySpawnHeight);
distanceSinceLastEnemy = 0f;
}
}
}
}
private Vector2 CalculateBezierPoint(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 endPoint)
{
return Mathf.Pow(1 - t, 3) * p0 + 3 * Mathf.Pow(1 - t, 2) * t * p1 + 3 * (1 - t) * Mathf.Pow(t, 2) * p2 + Mathf.Pow(t, 3) * endPoint;
}
}
TrackSegment.cs
using UnityEngine;
public class TrackSegment : MonoBehaviour
{
[Header("End Position")]
public Vector2 endPosition;
[Header("Control Points")]
public Vector2 controlPoint1;
public Vector2 controlPoint2;
public EdgeCollider2D edgeCollider;
public LineRenderer lineRenderer;
public float enemySpawnProbability = 0.5f;
public GameObject enemyPrefab;
public float minEnemySpawnDistance = 5f;
public float maxEnemySpawnDistance = 15f;
public float enemySpawnHeight = 3f;
public float gapProbability = 0.1f;
public float minGapSize = 2f;
public float maxGapSize = 5f;
}
About Genuinely Good Games
Play actaully fun web games you've never heard of. A collection of outstanding hidden gems, no login needed. The best place to play and discover actually fun, free online games.
Curated high quality games and hidden gems that you may have never found before.
More Games