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 MaleficVision · Aug 30, 2012 at 04:19 AM · androidraycastcollidersraycasting

Raycasting to make Buildings Clickable

I have a problem.

I used this code which I found somewhere here in the forums..it's a raycasting method for clicking objects.

` script ExecuteInEditMode

 var gSkin : GUISkin;
 var backdrop : Texture2D;
 var target1: Transform;
 var target2: Transform;
 private var isLoading = false;
 
 function OnGUI()
 {
  if (gSkin)
   GUI.skin  = gSkin;
  else
   Debug.Log("StartMenuGUI: GUI Skin object missing!");
  
 
 var backgroundStyle : GUIStyle = new GUIStyle();
 backgroundStyle.normal.background = backdrop;
 
 GUI.Label(Rect((Screen.width - (Screen.height *2)) *0.75,0,Screen.height *2,
 Screen.height ),"",backgroundStyle);
 
 
 //Loading Screen
 if (isLoading)
   Application .LoadLevel("Rotating Arch");
 }
 function Update () {
     if (Input.GetMouseButton(0)) {
         var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit: RaycastHit;
         
         if (Physics.Raycast(ray, hit)) {
             if (hit.transform == target1) {
                 print("Hit target 1");
                 isLoading = true;
             
             } else if (hit.transform == target2) {
                 print("Hit target 2");
             }
         } else {
             print("Hit nothing");
         }
     }
 }`


Anyway..

This works fine for those buildings that have single meshes. But sometimes when the model has multiple meshes...I choose the one that covers all parts of the model(that's where I put my collider) then test if it will take a "hit".

Apparently...it does not work.

NOTE: in the code sample there are 2 targets only. Because I test the buildings 1 by 1 first to know if I put the colliders in the right mesh. But in the long run...once I have all colliders in the right place I will have multiple targets.

The goal is to make each building "clickable" so that when it is "hit" it will go to a scene where it's details are shown.

The problem is that Multiple Meshed models seem to not work for this code. :( I need to fix this asap for this is part of my thesis XD

Did I do something wrong? Is there any other alternative for this kind of thing?

ANOTHER NOTE: I attach the above script to an FPS Controller. This is part of a "Free Roam" function of my thesis. More of a digital tour of a place :)

Comment
Add comment · Show 2
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 30, 2012 at 05:28 AM 0
Share

Have you ticked convex for the complex mesh collider?

Also, use a

 Debug.DrawLine(other.ScreenPointToRay(Input.mousePosition),hit.point,Color.white);

to check if the ray passes through or actually collides with the obejct.

avatar image MaleficVision · Aug 31, 2012 at 12:55 AM 0
Share

sorry for the late reply. anyway will try all of your suggestions and get back to you as soon as I can. Thanks! :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fattie · Aug 30, 2012 at 06:13 AM

are you familiar with the physics "layers" system in Unity3D ?

in may situations like this you'd simply make a set of separate colliders (likely simple boxes) for the use of the "click on a building" system.

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 MaleficVision · Aug 31, 2012 at 12:56 AM 0
Share

I've tried box colliders on each models...it won't work. I don't know why :(

avatar image MaleficVision · Sep 16, 2012 at 06:22 AM 0
Share

Sorry for the super late reply! Anyway I've managed to make the box collider work with the code. The problem was(I Think) is that the models have a mesh collider then I put over a box collider. I think it messes up the Raycast or something.

So I just removed the mesh colliders and then put the box colliders in the array of targets.

Thanks again for the help.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ray cast affects all colliders in scene if mouse button is not releasesd. 1 Answer

raycast screenpointtoray isnt working for some reason 0 Answers

How can I let the ray to the edge of the capsule? 1 Answer

Raycast not hitting the collider properly it always has a weird offset 0 Answers

RTS Object selection Ignoring Colliders 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