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 JakeParrott · Aug 04, 2013 at 11:43 AM · elevator

Elevator Script.. Please Help!

Hello Guys, I'm currently working on a game project with a few friends of mine and before I start putting the level together I would like to get basic functionalities of the level working. Up to now I've managed thanks to you wonderful people but now I've got stuck and I am looking to you for help! (as my programming skills suck)

I need an elevator working and so far I've got this code which works fine, however i would like to develop this script a bit further by adding some script that only makes this script valid when in a trigger zone. if you understand?

basically I only want this script to apply when I'm in the elevator, otherwise this script works even when I'm on the other side of the map!

Also I would like the whole animation to play before I can play it again.

Any and all help will be greatly appreciated by myself and my team, Thank you!

var isAtOriginalPosition = true; //Variable to control if the elevator is "up" or "down"

 function Update(){
     if(Input.GetKeyDown("e")){
         ActiveElevator();
     }
 }
 
 function ActiveElevator(){
     if(isAtOriginalPosition){
         animation["animationName"].time = 0;
         animation["animationName"].speed = 1;
         animation.Play("animationName");
         isAtOriginalPosition = false;
     }
     else{
         animation["animationName"].time = animation["animationName"].length;
         animation["animationName"].speed = -1;
         animation.Play("animationName");
         isAtOriginalPosition = true;
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Lovrenc · Aug 04, 2013 at 11:49 AM

Put interactive scripts on your player, not on objects you want to controll. That way, when user presses E, you can go trough objects in your proximity (be it by triggers, raycasting,...) and execute calls you need.

That way, you will greatly reduce number of Update functions, you will have "interaction" code on one place and it will be easy to execute a lot of different actions (depending on objects "E" keypress relates to).

About your question. If you will leave it as it is, you can use "Box collider (mark it as isTrigger)" on your elevator. When players triggers it, you remember it until he exits. So when "e" is pressed, if player is in the trigger zone you react, otherwise you exit function without any action. BUT THIS IS REALLY BAD DESIGN.

Comment
Add comment · Show 5 · 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 fafase · Aug 04, 2013 at 11:58 AM 0
Share

Why is it bad design to add a box collider? The way you explain means you control everything from one script,why not. But that kinda goes against the idea of each object controls its behaviours. If you need maintain your script, if something is wrong with the platform, you look for the platform, in your case, you look for the few lines among hundreds where you deal with the platform.

That means if a dog needs bark when you are around, you put that on your player script as well, right after the platform lines? That does not sound good to me.

avatar image fafase · Aug 04, 2013 at 12:03 PM 0
Share

I may have misunderstood...

avatar image Lovrenc · Aug 04, 2013 at 12:24 PM 0
Share

No, elevator still controlls its own actions. But he provides functions to be called and player calles them. It makes sense (in my opinion, but then again i am by no means great developer) because he wants the player to interact with it by pressing "E" key. So i don't see any meaning in elevator checking for keypress events.

Your dog example is totally different beast and would make no sense to force bark code in player script. If however player would need to "Give a treat" to a dog for him to bark by pressing "E", i would put:

$$anonymous$$eypress logic to player. Then check what object to apply it to and call "Interaction" interface function (and maybe send parameters if needed, depends on type and number of different actions). Then it is up to dog to do his part.

I might be in total darkness here so please comment on this further.

avatar image JakeParrott · Aug 04, 2013 at 12:26 PM 0
Share

Could you please further explain your method?

The way I'm understanding what you've said is that I should be applying the script to the player, and then through use of triggers it can detect the players input in the trigger area and if its a match (say E is to play the animation) it will play the animation, then once the player is out of the trigger area pressing E will no longer effect it?... is this correct or am I way off course? haha

avatar image Lovrenc · Aug 04, 2013 at 12:30 PM 0
Share

Plase wait, maybe my pattern is bad and i dont want to $$anonymous$$ch bad practices.

avatar image
0

Answer by JakeParrott · Aug 04, 2013 at 01:47 PM

http://www.youtube.com/watch?v=HfX6LsDGY2g

I am about to follow this guys video tutorials. its not exactly what I'm looking for, I would have preferred a key press function (maybe I could incorporate that into this somehow I'm not sure)

But thanks for your help :)

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Learning C# instead of doing what tutorials say... 2 Answers

C# Targetting Script 2 Answers

Need Coding Help! BCE0044: expecting }, found ''. 1 Answer

New to unity and any coding for that matter, need advice!!! 3 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