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 Kag359six · Jan 18, 2012 at 07:10 PM · raycasttriggerbugdetection

Trigger and raycast error?

Basically im starting an inventory script. I first tried using a raycast, which detects what object its looking through a tag, then returns the name of the gameObject. however, sometimes the ray doesnt pick up the ray. This happens as well while using a trigger. Everything within the code works once the ray can detect the object. The object being detected has a rigidbody component. heres the script:

 #pragma strict
 
 var selectedObjectName : String;
 
 var mainCamera : Transform;
 
 var cameraObj : Camera;
 
 var rayDistance : float = 10;
 
 var selectedObject : GameObject;
 
 var rayOn = false;
 
 var guiBackground : Texture;
 
 //GUI positions
 
 var guiX : float = 200;
 
 var guiY : float = 200;
 
 var guiHeight : float = 250;
 
 var guiWidth : float = 250;
 
 function Update () {
 
 var hit : RaycastHit;
 
 var ray = cameraObj.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0));
 
 Debug.DrawRay(ray.origin, mainCamera.transform.forward * rayDistance, Color.red);
 
 if(Physics.Raycast(ray.origin, ray.direction * 15, hit, rayDistance))
 {
     else if(hit.collider == null)
     {
         rayOn = false; // ray is off
     }
     
     if(hit.collider.gameObject.tag == "collectable") // if object with tag collectable is detected
     {
         rayOn = true; //then ray is on  <--- this is for checking whether ray is detecting object or not
         if(Input.GetMouseButtonDown(0))
         {
             selectedObjectName = hit.collider.name;
             Debug.Log("the selected object is called " + hit.collider.name);
             var selectedObject = GameObject.Find(selectedObjectName); // check for string name of current object
             Debug.Log("the actual object is " + selectedObject.name); //check to see if it was changed to gameobject
         }
     }
 }
 
 }

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 dannyskim · Jan 18, 2012 at 07:47 PM 1
Share

paste your code in from your selected coding application into the window, then highlight all of it and press the code button, which is the button that has "101010"

avatar image Kag359six · Jan 18, 2012 at 08:04 PM 0
Share

thanks! but now do you think you can help?

2 Replies

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

Answer by dannyskim · Jan 18, 2012 at 09:16 PM

 if(Physics.Raycast(ray.origin, ray.direction * 15, hit, rayDistance))
 {
     if(hit.collider.gameObject.tag == "collectable") // if object with tag collectable is detected
     {
        rayOn = true; //then ray is on  <--- this is for checking whether ray is detecting object or not
        if(Input.GetMouseButtonDown(0))
        {
          selectedObjectName = hit.collider.name;
          Debug.Log("the selected object is called " + hit.collider.name);
          var selectedObject = GameObject.Find(selectedObjectName); // check for string name of current object
          Debug.Log("the actual object is " + selectedObject.name); //check to see if it was changed to gameobject
        }
     }
 }
 else
     rayOn = false;

this is what i meant

Comment
Add comment · Show 3 · 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 Kag359six · Jan 18, 2012 at 09:20 PM 0
Share

THAN$$anonymous$$S A BUNCH FOR YOUR TI$$anonymous$$E! it worked. now i understand it better. thanks for sticking around.

avatar image dannyskim · Jan 18, 2012 at 09:24 PM 0
Share

Anytime =)

avatar image Kag359six · Jan 18, 2012 at 10:57 PM 0
Share

P.S if you can help with a GUI issue, it would be much appreciated. im posting the new question now

avatar image
0

Answer by dannyskim · Jan 18, 2012 at 08:09 PM

 var selectedObject = gameObject.Find(selectedObjectName);

Try changing the 'g' on gameObject to a capital G. So:

 var selectedObject = GameObject.Find(selectedObjectName);

I'll see if I can spot anything else in the meantime while you try that out.

Comment
Add comment · Show 9 · 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 Kag359six · Jan 18, 2012 at 08:12 PM 0
Share

thanks but thats not the problem. The meat of the code works, the issue is with the raycast hit itself, as it wont detect the object (collectable object in this case). It SO$$anonymous$$ETI$$anonymous$$ES detects the object, but not everytime.

avatar image dannyskim · Jan 18, 2012 at 08:20 PM 0
Share

are you sure your distance variable doesn't need to be increased for the raycast? Take for example if it's casting from the camera position and it's not casting directly in front of it, but at a diagonal, the distance will increase.

avatar image Kag359six · Jan 18, 2012 at 08:44 PM 0
Share

okay i centered the ray using screen pointToRay nstuff. The code should be updated. I have noticed now that the ray will detect the object towards the center of it, and the rayOn variable turns false whenever the ray interacts with another collider. HOWEVER it doesnt turn false when it touches nothing. How can i tell it to turn false when it touches nothing?

avatar image dannyskim · Jan 18, 2012 at 09:02 PM 1
Share

that should do it. try it out.

avatar image dannyskim · Jan 18, 2012 at 09:08 PM 1
Share

ahh I'm sorry, simple error on my part. When you take a look at your if( Physics.Raycast...) statement, that is technically checking if the physics.raycast returns true. When you look at the documentation, Physics.Raycast returns a boolean value of true or false. So you simple need to put the else statement after the if( Physics.Raycast) block to turn rayOn to false;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Object detection in front of Character 1 Answer

C# - Trigger Animation with Raycasting? 1 Answer

2d raycast enemy detection problem. 0 Answers

Raycast not detecting NavMeshAgent 1 Answer

Best Way to Find nearby colliders when you already have a trigger collider on gameObject 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