Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Queee · Jan 28, 2017 at 01:29 AM · scene-loadinginteraction

How do I switch scenes by pressing e on an object?

Basically the title but i already have on that works but i can press e at anytime and switch scenes. How can i press e only on an object to switch not just anywhere?

Heres what i have currently`

 using System;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class loadScene : MonoBehaviour
 {
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.E))
             SceneManager.LoadScene("DemoSceneFree");
     }
 }
 
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 HenryStrattonFW · Jan 28, 2017 at 11:56 AM

Well to do this you need to decide what it means to be "only on an object" I'm going to assume you mean, only when the mouse cursor is over an object? If that is the case then I would advise two mechanisms.

The first, make sure your loadScene object has a collider on it, then perform a raycast from the mouse screen position through the main camera into world space to see if it is over the loadScene object,

Alternatively to avoid doing the raycasts yourself you could still put a collider on the object and then use the OnMouseOver method of MonoBehaviours and OnMouseExit to detect the mouse entering and exiting the object, in these methods you could set a flag for example "isMouseOver" on the object, then change your if statement to include a check on that flag and only respond if it is true. The documentation for OnMouseEnter event can be found here https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html

Another thing that you may wish to incorporate is a distance check from the player to the object, as both of these methods will without restriction let you detect the mouse being over the object from much further away than you may want.

Hope this helps lead you in the right direction.

Comment
Add comment · Show 2 · 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 Queee · Jan 28, 2017 at 10:22 PM 0
Share

I dont know how to use raycasts, so can you give me a link or an example on how to do that ?

avatar image HenryStrattonFW Queee · Jan 28, 2017 at 11:21 PM 0
Share

https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

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

91 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 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 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 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 avatar image avatar image avatar image

Related Questions

Unity 5 Survival Shooter Crash 0 Answers

Passing Score from Game Scene to Game Over Scene usiing dontDestroyOnLoad C# 1 Answer

Any help 'updating' this script? 0 Answers

Game Crashing on Android when loading scene 4 Answers

Multiple Scenes for iOS 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