- Home /
Dynamic Stairs Generation
Hi all, I'm trying to create a staircase generator. I'll try my best to explain this as clearly as possible.
What I've done so far is made a set of stairs which has a fixed number of steps. These stairs are hidden in the ground. When a switch is triggered, the stair raises up step-by-step starting from the highest one. The stair is only walkable once the lowest step has reached its end position.
Photobucket link; in case the image is still not showing.
In the image above, stair highlighted in yellow is what I've managed to achieve. And highlighted in green is the current set up I have. Now I want to make it dynamic. It will generate a set of stairs based on the user's input of the width and height. The slope collision is needed because of how the player is coded.
Here is the script for the yellow highlighted stair:
using UnityEngine;
using System.Collections.Generic;
public class TriggerStairs : MonoBehaviour
{
public Transform endPoint;
public float moveSpeed = 1f;
private int stepCount = 0;
private float startTime = 0f;
private float transLength = 0f;
private List<Transform> stepArr;
private List<Vector3> stepStartPointsArr;
private List<Vector3> stepEndPointsArr;
private const int TOTAL_STEPS = 10;
private bool firstTimeCheck = true;
void Awake ()
{
stepArr = new List<Transform> ();
stepStartPointsArr = new List<Vector3> ();
stepEndPointsArr = new List<Vector3> ();
}
void Start()
{
stepArr.Add (transform.FindChild ("Step10"));
stepArr.Add (transform.FindChild ("Step9"));
stepArr.Add (transform.FindChild ("Step8"));
stepArr.Add (transform.FindChild ("Step7"));
stepArr.Add (transform.FindChild ("Step6"));
stepArr.Add (transform.FindChild ("Step5"));
stepArr.Add (transform.FindChild ("Step4"));
stepArr.Add (transform.FindChild ("Step3"));
stepArr.Add (transform.FindChild ("Step2"));
stepArr.Add (transform.FindChild ("Step1"));
foreach(Transform step in stepArr)
{
stepStartPointsArr.Add(step.localPosition);
stepEndPointsArr.Add(step.localPosition + endPoint.localPosition);
}
}
/**
* Lerps each step to its end position.
*/
public void Activate ()
{
if(firstTimeCheck)
{
startTime = Time.time;
firstTimeCheck = false;
}
if (stepCount < (stepArr.Count))
{
transLength = Vector3.Distance(stepStartPointsArr [stepCount], stepEndPointsArr [stepCount]);
float distCovered = (Time.time - startTime) * moveSpeed;
float fracJourney = distCovered / transLength;
stepArr[stepCount].localPosition = Vector3.Lerp (stepStartPointsArr[stepCount], stepEndPointsArr[stepCount], fracJourney);
if ( stepArr[stepCount].localPosition == stepEndPointsArr [stepCount])
{
++stepCount;
if (!(stepCount < (stepArr.Count)) )
{
return;
}
startTime = Time.time;
transLength = Vector3.Distance (stepStartPointsArr[stepCount], stepEndPointsArr[stepCount]);
distCovered = (Time.time - startTime) * moveSpeed;
// Fraction of journey completed = current distance divided by total distance.
fracJourney = distCovered / transLength;
}
}
}
}
Here is the code of the “yellow” example. I hope I was clear enough. The Activate() function is called in the scripts for the switch.
Thank you in advance for your help.
Edits:
Reuploaded image.Let me know if it's still not working.
Forgot to mention: I want to have multiple instances of this stair generator.
Added a Photobucket link to the image.
The image is broken. Re-upload it so we can have better understanding of the scenario.