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.
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();
}
}
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.
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:
I've also tried layermasks, doesn't work.
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?
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 :))
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
Follow this Question
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