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 kushagrakhare1991 · Oct 06, 2015 at 11:28 AM · unity 5triggerscene change

Unity look input to get detail and change the scene

I am new to unity..I want to ask that how to take action while looking on particular button or cube and then change the scene.

Look on object to see the hidden description of it.

Look on object and after 5 seconds loads the new scene.

I have made the whole room scene with chairs and tables in unity and I want to embed the above two points into that objects.

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
2

Answer by Statement · Oct 11, 2015 at 12:46 AM

Use Physics.Raycast to cast a ray from the Camera. See also Camera.ViewportPointToRay. (0.5, 0.5, 0) is in the middle of the screen with viewport coordinates. When the ray hit something you want to interact with, let it interact with it. It can be solved many ways. I chose to use SendMessage.

I have prepared a complete example for you to download and play around with. To get it to work, import the package and add the scene to build settings.

  • LookAtPointer.unitypackage download

LookAtPointer should be configured to at least have reference to your Camera. Whatever the camera points to that has a collider will get the messages OnLookEnter, OnLookStay and OnLookExit. There are some parameters to fine tune how it will work, but you can ignore them for now.

LookAtPointer.cs

 using UnityEngine;
 
 public class LookAtPointer : MonoBehaviour
 {
     public new Camera camera;
     public Vector2 pointer = new Vector2(0.5f, 0.5f);
     public float pointerDistance = float.PositiveInfinity;
     public LayerMask pointerMask = -1;
 
     private GameObject oldGO;
     private const SendMessageOptions STFU 
         = SendMessageOptions.DontRequireReceiver;
 
     void Update()
     {
         Ray ray = camera.ViewportPointToRay(pointer);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, pointerDistance, pointerMask))
         {
             GameObject newGO = hit.collider.gameObject;
             if (oldGO == newGO)
             {
                 oldGO.SendMessage("OnLookStay", STFU);
             }
             else
             {
                 if (oldGO)
                     oldGO.SendMessage("OnLookExit", STFU);
                 newGO.SendMessage("OnLookEnter", STFU);
             }
             oldGO = newGO;
         }
         else
         {
             if (oldGO)
                 oldGO.SendMessage("OnLookExit", STFU);
             oldGO = null;
         }
     }
 }

LookAtSetDescription will update text of a UI Text component whenever it is being looked at. The description text will be copied to that Text component. It requires to be attached to a game object with a collider on it. Optionally you can either set a reference to a Text component, or it will search the scene for a game object called "Description" and access its Text component.

LookAtSetDescription.cs

 using UnityEngine;
 using UnityEngine.UI;
 
 public class LookAtSetDescription : MonoBehaviour
 {
     public string description;
     public Text text; // Optionally, set text reference.
 
     Text safeText
     {
         get
         {
             if (text)
                 return text;
 
             var shared = GameObject.Find("Description");
 
             if (!shared)
             {
                 Debug.LogError("'Description' GameObject not found!", this);
                 return null;
             }
             else
             {
                 var sharedText = shared.GetComponentInChildren<Text>();
                 if (!sharedText)
                     Debug.LogError("Text Component not found on 'Description' GameObject!", this);
                 return sharedText;
             }
         }
     }
 
     void OnLookEnter()
     {
         safeText.text = description;
     }
 
 
     void OnLookExit()
     {
         safeText.text = string.Empty;
     }
 }

LookAtLoadLevel will load the level called levelName after continously looking delay seconds on the object. This is intentionally made so it won't accidentally load a level.

LookAtLoadLevel.cs

 using UnityEngine;
 
 public class LookAtLoadLevel : MonoBehaviour
 {
     public string levelName;
     public float delay = 1f;
 
     void OnLookEnter()
     {
         Invoke("LoadLevel", delay);
     }
 
     void OnLookExit()
     {
         CancelInvoke("LoadLevel");
     }
 
     void LoadLevel()
     {
         Application.LoadLevel(levelName ?? Application.loadedLevelName);
     }
 }


Comment
Add comment · Show 4 · 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 kushagrakhare1991 · Oct 12, 2015 at 11:01 AM 0
Share

thanks for the very nice example you made.....i want to make the pointer of the google cardboard crosshair to load animate and for few seconds and then switch to new scene like this YouTube video . In this youtube video Video link at time 0:50 sec how should i implement that thing ,i.e focus on icon and then switching to another scene.

avatar image Statement kushagrakhare1991 · Oct 12, 2015 at 06:21 PM 0
Share

In OnLookEnter, you could play an animation. Stop the animation in OnLookExit.

avatar image kushagrakhare1991 Statement · Oct 13, 2015 at 04:07 AM 0
Share

ok i want to load the crosshair should i use any .gif image or multiple images or something else could you provide any link of tutorial or any example bundle as you provided me earlier.Thanks in advance !!

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Find GameObject at Position even if the GameObject is a trigger. 0 Answers

Counting OnEnterStay or other methods 1 Answer

OnTriggerEnter Not working? 1 Answer

OnTriggerEnter2D called without collision 0 Answers

Help with Trigger Colliders.. 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