Detect if Raycast hit a trigger
Pretty simple, I have no idea how to check if the collider that a raycast hit is a trigger. How would I go about doing that? Preferably in C#.
Just checking, there are two separate things. One is how to fire a raycast that will hit or skip trigger (they always hit non-triggers.) That's done using the last parameter in a raycast (or the editor setting, which is old.) A standard raycast will never hit a trigger.
The other is what you're asking: checking trigger/not on what you hit. Obviously, that only matters if the raycast can hit triggers in the first place.
Answer by JoshDangIt · May 23, 2016 at 09:28 PM
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.isTrigger)
{
//Do the thing
}
}
Answer by tanoshimi · May 23, 2016 at 08:14 PM
Using the isTrigger property?
Yeah but how? From what I know, Raycasts return booleans, I don't know how to use IsTrigger on that. I really suck at raycasts.
Raycast return a RaycastHit, which has a collider property: http://docs.unity3d.com/ScriptReference/RaycastHit-collider.html
Test that to see if isTrigger is true.
Well, actually Raycasts do return a bool like soxroxr said: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
To get a RaycastHit you have to use the Raycast methods that have an "out" parameter of RaycastHit type, and that variable will be set after the raycast finishes. Then you can get the collider and check the isTrigger like JoshDangit did below (that one should be the accepted answer).
Thanks so much. Searched for hours to find a solution and in C#. Lots of posts that were close but this solved my issue. I had an object animation that was triggered by player ontrigger enter but I wanted it to trigger when hit by raycast AND when I hit my right mouse. Here is the script if anybody cares . i know I hate it when reading a posts and a user says "yippeee, I figured it out" without sharing
using UnityEngine; using System.Collections;
public class CameraRaycast : $$anonymous$$onoBehaviour {
Camera camera;
//private GameObject dancertest;
private $$anonymous$$onoBehaviour Dance;
Animation animation;
void Start() {
camera = GetComponent<Camera>();
Dance = GetComponent<danceranimater>();
animation = GameObject.Find("dancertest").GetComponent<Animation>();
}
void Update() {
Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
print("I'm looking at " + hit.transform.name);
{
if(hit.collider.isTrigger)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1))
animation.Play("Dance");
}
}
}
}
Answer by HinxLai · Sep 10, 2018 at 08:55 AM
my code:
using UnityEngine;
public class DM_RayCastTrigger : MonoBehaviour
{
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
if (Physics.Raycast(ray, out hitinfo))
{
print(hitinfo.transform.name + "::" + hitinfo.collider.isTrigger);
}
}
}
}
But I still don't know why physic raycast could detected an trigger. It's dosen't make sense.
Your answer
Follow this Question
Related Questions
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer
How to get raycast to ignore objects behind a panel 0 Answers
Check if someone is seeing an object and inside of a trigger? 0 Answers
Trigger animation when looking at an object? 1 Answer
How do I affect multiple objects in a single click? 0 Answers