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 djanthony93 · Oct 06, 2016 at 03:32 PM · triggerplayerraycasting

How to activate "OnTriggerEnter" when Player ray hits an object collider?

I have made a script for the Player to have a ray on the center of the camera 3 unit long, how can I raise the OnTriggerEnter function when my ray hits an object collider?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by chillersanim · Oct 06, 2016 at 04:40 PM

You shouldn't call the OnTriggerEnter method yourself, as this is a special method for when a trigger event happens.

If you want to execute the code on the player script, which you have in OnTriggerEnter, you should move that code to a seperate method, and call this method from the ray cast.

If you want to execute code on the object your raycast hit (not the player), then it needs a defined script you can search for, with a defined public method, which does whatever it should do when the object is hit.

Example:

 /// <summary>
 /// The players class
 /// </summary>
 public class Player : MonoBehaviour
 {
     void Update()    // Or FixedUpdate() for physical stuf
     {
         RaycastHit hit;
         if (Physics.Raycast(transform.position, transform.forward, out hit))
         {
             if (hit.collider != null)
             {
                 // Find the hit reciver (if existant) and call the method
                 var hitReciver = hit.collider.gameObject.GetComponent<HitReciver>();
                 if (hitReciver != null)
                 {
                     hitReciver.OnRayHit();
                 }
             }
         }
     }
 }
 
 /// <summary>
 /// The component that needs to be added to the other objects which need a callback on a ray hit.
 /// </summary>
 public class HitReciver : MonoBehaviour
 {
     // Called when the player ray hits this object
     public void OnRayHit
     {
         // Whatever you want to happen on a ray hit...
     }
 }

Greetings
Chillersanim

Comment
Add comment · Show 2 · 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 djanthony93 · Oct 06, 2016 at 09:48 PM 0
Share

Thank you so much.

avatar image sergiobd · Nov 17, 2017 at 05:09 PM 1
Share

What if you want to call the method when the raycast ENTERS the collider? I mean, only once?

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

93 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

Related Questions

What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 0 Answers

Croutch Player when Trigger enter 0 Answers

PLAYER NOT ACTIVATING TRIGGER WHEN ON TOP OF THE COLLIDER! IS THIS A BUG 0 Answers

My trigger won't activate the UI when the player collides with it. 0 Answers

How do I hide/show an object, when the player enters a trigger zone? 2 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