• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alieos · Feb 27, 2014 at 11:21 AM · c#prefabdynamicgenerator

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.

alt text

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:

  1. Reuploaded image.Let me know if it's still not working.

  2. Forgot to mention: I want to have multiple instances of this stair generator.

  3. Added a Photobucket link to the image.

screenshot.png (24.0 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CodeElemental · Feb 27, 2014 at 01:38 PM 0
Share

The image is broken. Re-upload it so we can have better understanding of the scenario.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Filling a scene with prefabs programmatically 2 Answers

Two exact objects behaving differently... *scratches head* 0 Answers

C# -- Build character unit from script 1 Answer

Multiple Cars not working 1 Answer

how can I create an array or IList of a specific prefab type in C# ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges