• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RainKiller27 · Oct 08, 2013 at 11:28 AM · blender

[Solved]Can anyone help me with elevator door script

Okay i have been working on a elevator script, i can't seem to get both doors to open at the same time one going one way the other the other way and with a mouse press hovered over a button I've been trying to learn c# but not much is working can anyone help me so i can learn to make my own scripts.

Comment
Add comment · Show 2
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 Stormizin · Oct 08, 2013 at 12:11 PM 0
Share

What you are trying to do? Can you be more specific?

avatar image RainKiller27 · Oct 08, 2013 at 01:10 PM 0
Share

Well i'm trying to make elevator double doors open simultaneously opposites with a key press like (E) or something also i would like to add a open and closing sound. also a sound for the button i have a script ii found on unity $$anonymous$$anuel but i can't seem to add the rest.

 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour {
     public Transform start$$anonymous$$arker;
     public Transform end$$anonymous$$arker;
     public float speed = 1.0F;
     private float startTime;
     private float journeyLength;
     public Transform target;
     public float smooth = 5.0F;
     void Start() {
         startTime = Time.time;
         journeyLength = Vector3.Distance(start$$anonymous$$arker.position, end$$anonymous$$arker.position);
     }
     void Update() {
         float distCovered = (Time.time - startTime) * speed;
         float fracJourney = distCovered / journeyLength;
         transform.position = Vector3.Lerp(start$$anonymous$$arker.position, end$$anonymous$$arker.position, fracJourney);
     }
 }  

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MFen · Oct 08, 2013 at 01:33 PM

Make sure to set up the properties in the editor (if you need help with that just comment and let me know) For the code below just add to gameController (or main camera) put the left door on doorL gameobject, then right door on doorR gameobject. set the door speed to .1 (or slower/faster depending on how you want) set limit l to -1 and limitr to 1

Then just press space to open and close the door.

 using UnityEngine;
 using System.Collections;
 
 public class openDoors : MonoBehaviour {
     public GameObject doorL;
     public GameObject doorR;
     public float doorLSpeed;
     public float doorRSpeed;
     public float limitL;
     private float limitLCounter;
     public float limitR;
     private float limitRCounter;
     
     public bool openDoorsBool;
     public bool closeDoorsBool;
     
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         if (Input.GetKeyUp(KeyCode.Space))
         {
             if (openDoorsBool)
             {
                 openDoorsBool = false;
                 closeDoorsBool = true;
             }
             else
             {
                 openDoorsBool  = true;
                 closeDoorsBool = false;
             }
         }
         
         if (openDoorsBool)
         {
             if (limitLCounter >= limitL)
             {
                 doorL.transform.position = new Vector3 (doorL.transform.position.x - doorLSpeed, doorL.transform.position.y, doorL.transform.position.z);    
                 limitLCounter -= doorLSpeed;
             }
             if (limitRCounter <= limitR)
             {
                 doorR.transform.position = new Vector3 (doorR.transform.position.x + doorRSpeed, doorR.transform.position.y, doorR.transform.position.z);
                 limitRCounter += doorRSpeed;
             }
         }
         
         if (closeDoorsBool)
         {
             if (limitLCounter <= 0)
             {
                 doorL.transform.position = new Vector3 (doorL.transform.position.x + doorLSpeed, doorL.transform.position.y, doorL.transform.position.z);    
                 limitLCounter += doorLSpeed;
             }
             if (limitRCounter >= 0)
             {
                 doorR.transform.position = new Vector3 (doorR.transform.position.x - doorRSpeed, doorR.transform.position.y, doorR.transform.position.z);
                 limitRCounter -= doorRSpeed;
             }
         }
     
     }
 }
 
Comment
Add comment · Show 11 · Share
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 RainKiller27 · Oct 09, 2013 at 02:52 AM 0
Share

Aw thank you i set up the script with both doors, script is attached to my character but I have no idea how to set up the rest would you be able to help me out please? also sorry it's moving on the x axis how do i change it to the z axis.

avatar image MFen · Oct 09, 2013 at 12:47 PM 0
Share

Ok so to utilize this script, you just need attached to a single game object in the scene with the doors, then on click on that gameobject and look at the script. Fill in the script public variables (doorL/doorR).

To make it work on the Z axis you will need to open the script and where

 if (openDoorsBool)
         {
             if (limitLCounter >= limitL)
             {
                 doorL.transform.position = new Vector3 (doorL.transform.position.x, doorL.transform.position.y, doorL.transform.position.z - doorLSpeed);    
                 limitLCounter -= doorLSpeed;
             }
             if (limitRCounter <= limitR)
             {
                 doorR.transform.position = new Vector3 (doorR.transform.position.x, doorR.transform.position.y, doorR.transform.position.z + doorRSpeed);
                 limitRCounter += doorRSpeed;
             }
         }
         
         if (closeDoorsBool)
         {
             if (limitLCounter <= 0)
             {
                 doorL.transform.position = new Vector3 (doorL.transform.position.x, doorL.transform.position.y, doorL.transform.position.z+ doorLSpeed);    
                 limitLCounter += doorLSpeed;
             }
             if (limitRCounter >= 0)
             {
                 doorR.transform.position = new Vector3 (doorR.transform.position.x, doorR.transform.position.y, doorR.transform.position.z- doorRSpeed);
                 limitRCounter -= doorRSpeed;
             }
         }

Just change the z axis portion.

avatar image RainKiller27 · Oct 09, 2013 at 01:19 PM 0
Share

Aw thank!!! don't mean to be a pain but ins$$anonymous$$d of just adding a new script, how would i add a button beside the elevator doors and make the script to hover the mouse over and click to open up the doors also change color of the button when the mouse hovers over it. or the souse of where you learnt unity c# i have watch all the videos twice on unity page but it's not sinking in.

avatar image MFen · Oct 09, 2013 at 02:24 PM 0
Share

Go to youtube and look up custom guiskin. Utilizing your own skin vs. the built in one will be much better for what you want. You would need to do this for your hover state to change the button color when mouse over. To add a button do this

 if(GUI.Button(new Rect (0,0,50,50), "Test Button"))
 {
      Debug.Log("I Hit the button");
 }

To $$anonymous$$ake a custom button

     if(GUI.Button(new Rect (0,0,50,50), "Test Button", gamestyle.getStyle("$$anonymous$$yButtonTexture")))
     {
          Debug.Log("I Hit the button");
     }
avatar image RainKiller27 · Oct 09, 2013 at 02:45 PM 0
Share

where would i put that script in a new one or in the script you already have done. i have the button model done also will i need to link the two scripts together? sorry just having a bit a of trouble with this bit...

Show more comments

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

18 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

Related Questions

A node in a childnode? 1 Answer

How to animate inside Unity? (Blender animation is finished) 1 Answer

Animation Problems 1 Answer

Texturing only one face? 1 Answer

Importing .fbx Character in Blender 2 Answers


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