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 Carlos900 · Dec 18, 2019 at 11:05 AM · raycastraycastingtagraycasthittags

Raycast not working (NO ERRORS!)

I am trying to make a simple script that takes the input (space), makes a raycast to an object with a certain tag and plays a sound.

The output in the console:

 Start
 Update (Multiple times, obviously)
 SpacePressed
 RayCasted

BUT, it doesn't output HitSomethingWithTag and doesn't play the sound either.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BTNCS : MonoBehaviour
 {
 
     public AudioSource btnclicksoundsource;
     public Camera MainCamera;
 
     // Start is called before the first frame update
     void Start()
     {
         Debug.Log("Start");
     }
 
     // Update is called once per frame
     void Update()
     {
         Debug.Log("Update");
         if(Input.GetKeyDown("space")) {
             RaycastHit hit;
             Debug.Log("SpacePressed");
             if (Physics.Raycast (MainCamera.transform.position, MainCamera.transform.forward, out hit)) {
                 Debug.Log("RayCasted");
                 if (hit.transform.tag == "clickable_button") {
                     Debug.Log("HitSomethingWithTag");
                     btnclicksoundsource.Play();
                 }
             }
         }
     }
 }
 

I checked the editor multiple times and everything is set up correctly, no orthographical errors in the tagging, all public (external, set in the editor) variables set up. My first guess was that something was in the way of the camera, obstructing the tagged objects, but I checked. Please help me. EDIT: I've tried layermasks and tags, still doesn't work. It's like if Unity ignores it.

Comment
Add comment · Show 1
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 Hellium · Dec 18, 2019 at 11:56 AM 0
Share

If you get RayCasted in the console, it means the raycast is working and you have hit something.

  if (Physics.Raycast ($$anonymous$$ainCamera.transform.position, $$anonymous$$ainCamera.transform.forward, out hit)) {
      Debug.Log("Ray hit object tagged "+ hit.transform.tag + ". Click this message to highlight hit object", hit.transform );
      if (hit.transform.tag == "clickable_button") {
          Debug.Log("HitSomethingWithTag");
          btnclicksoundsource.Play();
      }
  }

1 Reply

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

Answer by TheCellCH · Dec 18, 2019 at 11:12 AM

Does your object have a collider set?

Also consider using Layermasks for the raycasting. Look into the Raycast manual for this (https://docs.unity3d.com/ScriptReference/Physics.Raycast.html) you can set a layermask in the inspector and have the raycasts only honor objects that are on this layermask.

Edit: So as @Carlos900 figured: The solution was to use hit.collider.tag because hit.transform.tag referenced the parent object.

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 Carlos900 · Dec 18, 2019 at 11:15 AM 0
Share

Thanks! The tiny irritating details you don't notice while program$$anonymous$$g something can be quite frustrating... Edit: Yes, it has a collider, still don't know the problem... This is how I set it up: alt text

I've also tried layermasks, doesn't work.

sdfsdfsdfsdfsdfsdf.png (9.2 kB)
avatar image TheCellCH Carlos900 · Dec 18, 2019 at 12:42 PM 0
Share

I see. The cast parameters look good so far, nothing is in the way of it that gets hit ins$$anonymous$$d? You can debug and check the ray direction with Debug.Drawray (https://docs.unity3d.com/ScriptReference/Debug.DrawRay.html) Can you screenshot the entire inspector view of the object?

avatar image Carlos900 TheCellCH · Dec 18, 2019 at 05:20 PM 0
Share

I noticed something interesting:

First, I modified the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BTNCS : $$anonymous$$onoBehaviour
 {
 
     public AudioSource btnclicksoundsource;
     public Camera $$anonymous$$ainCamera;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.Get$$anonymous$$eyDown("space")) {
             RaycastHit hit;
             Physics.Raycast($$anonymous$$ainCamera.transform.position, $$anonymous$$ainCamera.transform.forward, out hit);
             Debug.Log(hit.transform.tag);
         }
     }
 }

 

Then, I checked the output:

Untagged

Then, I looked into the inspector of the button, clearly tagged "clickable_button"! (Here's the screenshot, by the way :))

I$$anonymous$$AGE

avatar image Carlos900 · Dec 18, 2019 at 05:51 PM 1
Share

TheCellCH, I have got good news! I've figured out the problem! It was giving "Untagged" because it was redirecting to the PARENT's tag, so I used hit.collider.tag ins$$anonymous$$d.

Thank you for your 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

244 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 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 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

RayCast2D and RayDraw errors 0 Answers

Physics.Raycast Not Working... Kinda 0 Answers

How would I project a Gameobject onto the surface of another? 0 Answers

raycasting doesn't work 1 Answer

Accessing Raycast collider from another script 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