- Home /
How can I add more stairs to the specific group of stairs without changing its position ?
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class GenerateStairs: MonoBehaviour
{
[Header("Stairs Prefb")]
public GameObject stairsPrefab;
[Space(5)]
[Header("Platforms")]
public bool addPlatforms = false;
public GameObject platformsPrefab;
[Space(5)]
[Header("Settings")]
[Range(0,5)]
public float delay = 3;
[Range(5,100)]
public int stairsNumber = 5;
public Vector3 stairsStartPosition;
public Vector3 stairSize;
public Vector3 stairsSize;
public float stepWidthFactor = 1f;
private Vector3 stairsPosition;
private int oldStairsNumber;
private MoveStairs moveObjects;
// Use this for initialization
void Start()
{
oldStairsNumber = stairsNumber;
moveObjects = gameObject.GetComponent<MoveStairs>();
StartCoroutine(BuildStairs(moveObjects));
}
// Update is called once per frame
void Update()
{
if(oldStairsNumber < stairsNumber)
{
AddStairs();
}
oldStairsNumber = stairsNumber;
}
private IEnumerator BuildStairs(MoveStairs moveObjects)
{
for (int i = 1; i <= stairsNumber; i++)
{
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);
stairsPosition = stairsPosition + transform.localPosition;
GameObject stair = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
stair.name = "Stair";
stair.tag = "Stair";
stair.transform.parent = transform;
stair.transform.localScale = stairSize;
moveObjects.objectsToMove.Add(stair);
yield return new WaitForSeconds(delay);
}
moveObjects.enabled = true;
moveObjects.Init();
}
private void AddStairs()
{
for (int i = oldStairsNumber; i < stairsNumber; i++)
{
stairsPosition = new Vector3(
stairsStartPosition.x,
stairsStartPosition.y + (i * stairsSize.y),
stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);
stairsPosition = stairsPosition + transform.localPosition;
GameObject stair = Instantiate(
stairsPrefab,
stairsPosition,
Quaternion.identity);
stair.name = "Stair";
stair.tag = "Stair";
stair.transform.parent = transform;
stair.transform.localScale = stairSize;
//moveObjects.objectsToMove.Add(stair);
//moveObjects.Init();
}
}
If I'm not using the two lines moveObjects.objectsToMove.Add(stair); and moveObjects.Init(); it will add the stairs but the new added stairs will be static. But if I will use this two lines the new added stairs will also move but it will also change the whole stairs position.
This is a screenshot when not using this two lines after added new stairs:
And this screenshot when using the two lines after added new stairs:
Now the stairs are moving but not in the same position the stairs was before. The stairs now are moving much faster then the others. And there is no gap's spaces between the stairs like there was before. Gaps I mean on Stairs 0 like in the first screenshot.
This is the MoveStairs script that move the stairs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MoveStairs : MonoBehaviour
{
public List<GameObject> objectsToMove = new List<GameObject>();
public AnimationCurve curve;
public float stepsPerSecond = 1f;
public bool changeDirection = false;
private Vector3 trackStart;
private Vector3 trackEnd;
private Vector3 horizontalTravel;
private float verticalTravel;
private float divisor;
private float phase = 0f;
// Use this for initialization
public void Init()
{
if (curve == null)
{
curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
}
curve.preWrapMode = WrapMode.Clamp;
curve.postWrapMode = WrapMode.Clamp;
trackStart = objectsToMove[0].transform.localPosition;
int count = objectsToMove.Count;
var span = objectsToMove[count - 1].transform.localPosition - trackStart;
divisor = 1f / count;
horizontalTravel = (count + 1) * span * divisor;
horizontalTravel.y = 0f;
verticalTravel = span.y;
trackEnd = trackStart + (count + 1) * span / count;
}
// Update is called once per frame
void Update()
{
if (objectsToMove != null && objectsToMove.Count > 0 && curve != null)
{
AnimationCurve();
}
}
private void AnimationCurve()
{
phase = Mathf.Repeat(phase + stepsPerSecond * divisor * Time.deltaTime, 1f);
for (int i = 0; i < objectsToMove.Count; i++)
{
float t = Mathf.Repeat(phase + i * divisor, 1f);
// Get the height of the curve at this step.
float curveHeight = curve.Evaluate(t) * verticalTravel;
if (changeDirection)
{
objectsToMove[i].transform.localPosition = trackStart // First step
- horizontalTravel * t // evenly spaced horizontal
+ curveHeight * Vector3.up; // curving vertical
}
else
{
objectsToMove[i].transform.localPosition = trackStart // First step
+ horizontalTravel * t // evenly spaced horizontal
+ curveHeight * Vector3.up; // curving vertical
}
}
}
}
The main goal is when the game is running in real time when changing the stairs number value: Up or Down add/destroy new stairs for the specific group of stairs.
public int stairsNumber = 5;
I thought to do something like create two sockets on each stair , one at top and and one at the bottom, create a prefab , and keep adding or deleting using a list. But I'm not sure how to do it or if it's the best way to do it.
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i pick two randomly items from gameobject array ? 1 Answer
Script for a hue rainbow scroll on the material 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers