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 janoman · Mar 27, 2012 at 02:44 PM · navigationchild-to-gameobject

button as gameobject child

Hi!!!

I have being hitting my head with a problem and I'm in desperate need of help...

I need to use 3d objects inside a GameObject as buttons (go to game, go to x place... etc...) For an Android system (So, based on touch)

When I finally made the system check the collision between my finger and the damn button... all four buttons do what one of them is doing... I'll explain... if I have 4 buttons that go to different application.LoadLevel... It does not matter which one I press... al go to the same one.

Code as follows.

 function Update () 
 {
     for (var touch : Touch in Input.touches) 
     {
         if (touch.phase == TouchPhase.Began) 
         {
            var ray = Camera.main.ScreenPointToRay (touch.position);
            if (Physics.Raycast (ray)) 
            {
            Application.LoadLevel("introduccion");
            }
         }
     }
 }

Code works... But all 4 buttons have different programming ( Application.LoadLevel("Different Level") ) and the other 3 buttons redirect to "introduccion" anyway.

Those 4 scripts are attached to the childs of a GameObject (that can be a problem, I Know, but I have no choice (Using Metaio Mobile SDK and need to place al the AR 3D objects that are going to be shown in a single gameObject)

Thanks!

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 moichezmoi · Mar 27, 2012 at 08:07 PM

Hi,

The problem, here, is that Physics.Raycast(ray) will return true if the ray detects any collider, from any gameobject, and not only the collider of the gameobject the script is attached to.

To check the collider, you need to change your code as followed :

     if (touch.phase == TouchPhase.Began) 
     {
        var ray = Camera.main.ScreenPointToRay (touch.position);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit, 100))
        {
           //here, we check if the detected collider is the one attached to the gameobject
           if (hit.collider == collider)
               Application.LoadLevel("introduccion");
        }

Now, I'm not sure about the syntax because I'm not used to javascript, but the exemple given at the very bottom of this page is quite similar.

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

Answer by janoman · Apr 26, 2012 at 06:45 PM

Hi!

Thanks, that was indeed the error... I could not make it to the right javascript syntax... So I did implement a not to elegant solution, but it works...

This is it.

function Update ()

{

 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 var hit : RaycastHit;
 if (Physics.Raycast(ray.origin, ray.direction, hit))
 {
     if (Input.GetMouseButtonDown(0))
     {
         if(hit.collider.tag == ("btnup"))
         {
             Application.LoadLevel("introduccion");
         }
         if(hit.collider.tag == ("btnleft"))
         {
             Application.LoadLevel("cata");
         }
         if(hit.collider.tag == ("btnright"))
         {
             Application.LoadLevel("maridaje");
         }
         if(hit.collider.tag == ("btnbottom"))
         {
              Application.OpenURL ("http://www.x.com/");
         }
     }
 }

}

Thanks again, and sorry for the delay in answering, but my work has me swarmed :S

Good luck!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Navagation Ground Mapping 1 Answer

TextMeshPro Dropdown navigation gamepad problem 0 Answers

How to navigate within large hierarchies in the scene 1 Answer

NavMesh Obstacles carve to area other than non-walkable? 0 Answers

Making an object from scene a parent of a instantiated prefab 1 Answer


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