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
3
Question by soxroxr · May 23, 2016 at 08:10 PM · raycasttrigger

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#.

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 Owen-Reynolds · May 24, 2016 at 02:17 PM 0
Share

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.

4 Replies

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

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
             }
         }
Comment
Add comment · 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
3

Answer by tanoshimi · May 23, 2016 at 08:14 PM

Using the isTrigger property?

Comment
Add comment · Show 5 · 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 soxroxr · May 23, 2016 at 08:40 PM 0
Share

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.

avatar image tanoshimi soxroxr · May 23, 2016 at 09:01 PM 2
Share

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.

avatar image DiegoSLTS tanoshimi · May 24, 2016 at 02:12 AM 1
Share

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).

Show more comments
avatar image shadowpuppet · May 28, 2016 at 06:05 PM 2
Share

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");
         }
     }
 }

}

avatar image
0

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.

Comment
Add comment · 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
0

Answer by $$anonymous$$ · Nov 29, 2019 at 05:17 PM

isent it because of the queryTriggerInteraction?

Comment
Add comment · 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

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

6 People are following this question.

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

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


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