• 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 TStorm · Dec 07, 2014 at 10:26 PM · physics

How to Load Level on Button Press (Door)

Hi all,

Brand new Unity user as of this week, and am working on my first game. So far I have only done what I could accomplish in the editor alone, and am now trying to learn scripting. Now, let me preface this by saying that I HAVE checked previous answers to this problem. I have tried each and every one, but with no luck.

Level 0 is a hallway. I have a game object that is a hall with doorways. I created a cube and fit it to one of those doorways. I attached a box collider and checked "Is Trigger. I want the player to walk up to this door, hit a key, and load the next scene.

I found the following script in a previous answer, but it has not worked for me. Any help would be much appreciated, and thank you for your patience.

 public class Door10LoadLevel : MonoBehaviour {
     
         void OnTriggerEnter(Collider other) {
             if (other.gameObject.name == "Player"){
                     if (Input.GetKey ("F")){
                             Application.LoadLevel ("LevelName");
 



  1. Am I incorrect in assuming that this script should be placed on the door?

  2. Does the player also need a collider attached, or does the trigger recognize the player game object regardless?

  3. Does my player need a script attached?

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
Best Answer

Answer by screenname_taken · Dec 07, 2014 at 10:29 PM

On trigger enter is only fired the moment you enter that trigger. It only checks for a single frame, so unless you are a computer you won't be able to press the f button. Change it into OnTriggerStay, so that the trigger will keep checking for input for as long as the player is in the trigger. Leave the script on the trigger you made.

Also, put a collider on your player.

edit It seems converting my comment to an answer bugged out...

Comment
Add comment · Show 1 · 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 TStorm · Dec 07, 2014 at 11:15 PM 0
Share

screenname_taken: That did it, thank you. I've been trying to figure this out for two days. And it was that simple.

static_cast: Thanks, I downloaded the new beta Sample Assets and that controller had the rigidbody and collider built in. When I added a rigid body to the original controller it did crazy thing to my player.

avatar image
0

Answer by static_cast · Dec 07, 2014 at 10:30 PM

First, you should use OnTriggerStay(), so the check occurs every frame. Here are the answers to your questions. ;)

1) You can, but since your player probably takes input, you could put it there and see if the player collides with an object tagged door.

2) Yes, he does.

3) No, but one object needs a rigidbody.

So basically, you need the the door and the player to have colliders, and the player to have a rigidbody. [you should be using Physics to move the player probably anyway]

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

Flying back in car seat 1 Answer

OnCollision events are not always fired 2 Answers

Rigidbodies get huge amount of velocity when clipping into other Rigidbodies 2 Answers

Fist Passing Through The Target 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