• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ivissu · Aug 10, 2015 at 04:57 AM · raycastontriggerexit

Something like OnTriggerExit for Raycast?

Hi! I'm working in a 2d Puzzle game and I'm looking for something like OnTriggerExit for the Raycast. I'm making a script: I have a few cubes making the rol of walls, and a single player cube. The player cube have 4 diagonal raycast for each edge. When the player pass side of a cube wall any of the 4 raycast will detect that cube wall and will set isTrigger = true, to that cube wall. This keeps the player movement naturally, because it wont stop at the wall edge when hits. What i'm looking for is: When the raycast out of the cube wall set the cube wall isTrigger return to false.

(The Wall cubes have already "Wall" tag, and the Script already works for the 'isTrigger = true' when hits (All the Walls and cubes are 3D)

I need help, because the 'else' don't works :/ at least how I want

Sorry, I hope you haven't been confused :P

Thanks!

Script:

 RaycastHit hit;
         Ray Sup_izq = new Ray(Ray_Sup_izq.position, new Vector3(-1f, 0f, 1f));
         Ray Sup_der = new Ray(Ray_Sup_der.position, new Vector3(1f, 0f, 1f));
         Ray Inf_izq = new Ray(Ray_Inf_izq.position, new Vector3(-1f, 0f, -1f));
         Ray Inf_der = new Ray(Ray_Inf_der.position, new Vector3(1f, 0f, -1f));
 
         Debug.DrawRay(Ray_Sup_izq.position, new Vector3(-1f, 0f, 1f), Color.magenta, 1f);
         Debug.DrawRay(Ray_Sup_der.position, new Vector3(1f, 0f, 1f), Color.blue, 1f);
         Debug.DrawRay(Ray_Inf_izq.position, new Vector3(-1f, 0f, -1f), Color.cyan, 1f);
         Debug.DrawRay(Ray_Inf_der.position, new Vector3(1f, 0f, -1f), Color.green, 1f);
 
         if (Physics.Raycast(Sup_der, out hit, 3f) || Physics.Raycast(Sup_izq, out hit, 3f) || Physics.Raycast(Inf_izq, out hit, 3f) || Physics.Raycast(Inf_der, out hit, 3f))
         {
             if (hit.collider.gameObject.tag == "Wall")
             {
                 hit.collider.isTrigger = true;
                 //si esta aqui es porque el raycast detecto una pared
                 print("choque con " + hit.collider.name);
             }
         }

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 Lo0NuhtiK · Aug 10, 2015 at 05:00 AM 0
Share

(The Wall cubes have already "Wall" tag, and the Script already works for the 'isTrigger = true' when hits (All the Walls and cubes are 3D) I need help, because the 'else' don't works :/ at least how I want

What script? Post it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nabeel Akhtar · Aug 10, 2015 at 05:21 AM

You should attach raycast to walls and detect player is arround or not, its better way.

otherwise you have to keep track of all walls, so in the case of no detection you can set there trigger off

if(Physics.Raycast(ray, out hit,deplovementheight)) {

    if(hit.collider.tag == "Player"){
        this.GetComponent<Collider>().isTrigger = true;
    }
        else
        {
           //if it collides but its not Player
           this.GetComponent<Collider>().isTrigger = fasle;
        }
 

}

//if it collides with nothing

else {

  this.GetComponent<Collider>().isTrigger = fasle;

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Something like OnTriggerExit for Raycasts? 2 Answers

Nail a rigidbody to another 0 Answers

General Way To 'Widen' A Ray Cast? 1 Answer

Reload clips on Gun-script not applied properly 1 Answer

change object material on mouse click. 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