Skip to content

Instantly share code, notes, and snippets.

@RC0D3
Created January 8, 2024 02:04
Show Gist options
  • Save RC0D3/ae3197a5130db16b6fb177ab63756e23 to your computer and use it in GitHub Desktop.
Save RC0D3/ae3197a5130db16b6fb177ab63756e23 to your computer and use it in GitHub Desktop.
Script for Tiago my friend - Rotate objects on curve of infinity runner game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnTile : MonoBehaviour
{
public GameObject tileOnLine;
public GameObject tileOnCurve;
public GameObject referenceObject;
public float timeOffset = 0.4f;
public float distanceBetweenTiles = 5.0F;
[Range(0, 100)]
public int porcentageToChangeCurve = 20;
private Vector3 previousTilePosition;
private float startTime;
private Vector3 currentDirection, mainDirection = new Vector3(0, 0, 1), otherDirection = new Vector3(1, 0, 0);
private Vector3 objectRotation = new Vector3(0, 0, 0);
private Vector3 curveRotation = new Vector3(0, 0, 0);
private bool directionChanged = false;
// Start is called before the first frame update
void Start()
{
previousTilePosition = referenceObject.transform.position;
startTime = Time.time;
}
// Update is called once per frame
void Update()
{
if (Time.time - startTime > timeOffset)
{
GameObject objectToSpawn = tileOnLine;
// Check if will change direciton
if (Random.value < ((100 - (float)porcentageToChangeCurve) / 100) && !directionChanged)
currentDirection = mainDirection;
else if (directionChanged)
{
Vector3 temp = currentDirection;
currentDirection = otherDirection;
mainDirection = currentDirection;
otherDirection = temp;
// Change next roration of object
objectRotation.y = objectRotation.y + 90;
directionChanged = false;
}
else
{
//Before change direction, create a curve for this.
objectToSpawn = tileOnCurve;
directionChanged = true;
}
Vector3 spawnPos = previousTilePosition + distanceBetweenTiles * currentDirection;
startTime = Time.time;
if (objectToSpawn == tileOnLine) //When tile of line, spawn with rotation
{
Instantiate(objectToSpawn, spawnPos, Quaternion.Euler(objectRotation.x, objectRotation.y, objectRotation.z));
}
else // Else instantiate with default rotation
{
Instantiate(objectToSpawn, spawnPos, Quaternion.Euler(curveRotation.x, curveRotation.y, curveRotation.z));
//Change curve more rottation
curveRotation.y = curveRotation.y + 180;
}
previousTilePosition = spawnPos;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment