Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 svist_o2 · Mar 16, 2012 at 02:21 PM · javascriptmovingelevator

Elevator script problem (Java)

Hi all, I am new to Unity! I do lift. When you press E, the elevator should move to the next floor (it works), but when you reach the top floor, the elevator continues engines on. Please help fix it. How to make the elevator did not move further up on reaching an endpoint, even when you press E, and how to achieve smooth motion lift?

Please help me! Thank you!

Here's a script that I wrote (elevator should be located initially in the zero-point coordinates):

 var elevator: Transform;
 var player: Transform;
 private var nextPuch: float = 0.5;
 private var eleRate : float = 5.0;
 private var curPos: Vector3;
 
 function OnTriggerEnter () {
 var GO = elevator;
 var GO1 = player;
     GO1.transform.parent = GO.transform;
 }
  
 function OnTriggerExit () {
 var GO = elevator;
 var GO1 = player;
     GO1.transform.parent = null;
 }
 
 function Update () {
 curPos = transform.position;
 
 var eleHeight = Vector3(0, 5, 0);
 
 //elevator should be located initially in the zero-point coordinates
 var floor01 = Vector3(0, 0, 0);
 var floor02 = Vector3(0, 5, 0);
 var floor03 = Vector3(0, 10, 0);
 var floor04 = Vector3(0, 15, 0);
 var floor05 = Vector3(0, 20, 0);
 
 if (curPos != floor01 && Input.GetKey(KeyCode.E) && Time.time > nextPuch) {
     elevator.transform.position = curPos + eleHeight;
     nextPuch = Time.time + eleRate;
     }
 else if (curPos != floor02 && Input.GetKey(KeyCode.E) && Time.time > nextPuch) {
     elevator.transform.position = curPos + eleHeight;
     nextPuch = Time.time + eleRate;
     }
 else if (curPos != floor03 && Input.GetKey(KeyCode.E) && Time.time > nextPuch) {
     elevator.transform.position = curPos + eleHeight;
     nextPuch = Time.time + eleRate;
     }
 else if (curPos != floor04 && Input.GetKey(KeyCode.E) && Time.time > nextPuch) {
     elevator.transform.position = curPos + eleHeight;
     nextPuch = Time.time + eleRate;
     }
 else if (curPos != floor05 && Input.GetKey(KeyCode.E) && Time.time > nextPuch) {
     elevator.transform.position = curPos;
     nextPuch = Time.time + eleRate;
     }
 return;
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by armoredpokey · Mar 18, 2012 at 06:00 PM

My best guess is that curPos may not exactly equal floor05 -- there might be some kind of rounding error. Instead of checking whether curPos != various things, I would check whether it's less than or equal to the different floor values. Also, I would refactor the code so you aren't checking Input & Time 5 times every time the code executes, and only compare the y-position values instead of the entire vector.

So it might look something like this:

if (Time.time > nextPuch && Input.GetKey(KeyCode.E)) {

    if (curPos.y <= floor01.y) {

       // your logic

    } else if (curPos.y <= floor02.y) {

        // more logic

    }

    etc...

}

return;

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Disable GameObject Only Father Not Children 2 Answers

GUI.Box within a Collider 1 Answer

Why is my script still running in other scenes? 1 Answer

Light problems 1 Answer

Getting Error "No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found." 0 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