Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
1
Question by FudgePackingPro · May 18, 2020 at 10:50 PM · scripting problemtriggersboxcolliderthrowingpick up object

How to stop certain gameobjects from interacting with triggers

I'm having a problem with my Item Pick up script where the trigger collider activates on every single gameobject it hits, this is a problem because it stops the player from picking up other game objects with this script. Is there anyway I can fix this or exclude certain game objects from activating the trigger?

My code:

 public class betterPickup : MonoBehaviour
 {
     
     public Transform player;
     public Transform playerCam;
     public float throwForce = 500;
     bool hasPlayer = false;
     bool beingCarried = false;
     private bool touchedWall = false;
    
     // Update is called once per frame
     void Update()
     {
         //checks the distance between the object being picked up and the player
         float dist = Vector3.Distance(gameObject.transform.position, player.position);  
         /*if the distance is less than 5 units, the object can be picked up, this is to stop the script from
         selecting of all game objects that use this script*/
         if(dist <= 5f)
         {
             hasPlayer = true; 
         }
         else
         {
             hasPlayer = false; 
         }
         if(hasPlayer && Input.GetKeyDown("e"))
         {
             //if the object can be picked up & the player presses E, the object is parented to the player camera
             //isKinematic is true so the game object doesn't fall down once picked up 
             GetComponent<Rigidbody>().isKinematic = true;
             transform.parent = playerCam;
             beingCarried = true; 
         }
         if (beingCarried)
         {
             if (touchedWall)
             {
                 //if the object touches a wall, the player stops carrying the object
                 GetComponent<Rigidbody>().isKinematic = false; 
                 transform.parent = null;
                 beingCarried = false;
                 touchedWall = false; 
             }
             if (Input.GetMouseButtonDown(0))
             {
                 //throwscript
                 
                 /*same as touchedWall except we add force to the object via playercam forward speed
                   multiplied by the throwForce so the object go fast*/
                 
                 //tldr: object go fast if left mouse do clicky clicky
                 GetComponent<Rigidbody>().isKinematic = false;
                 transform.parent = null;
                 beingCarried = false;
                 GetComponent<Rigidbody>().AddForce(playerCam.forward * throwForce);
             }
             else if (Input.GetMouseButtonDown(1))
             {
                 //drop script
                 //right mouse to drop object
                 GetComponent<Rigidbody>().isKinematic = false;
                 transform.parent = null;
                 beingCarried = false;
             }
         }
     }
     void OnTriggerEnter()
     {
         //if the object hits anything, it sets touchedWall to True 
         //trigger is used because disabling isKinematic also disables collision
         if (beingCarried)  
         {
             touchedWall = true;
         }
     }
 }
 




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
Best Answer

Answer by blinkafrootable · May 18, 2020 at 10:59 PM

There are two different ways that come to mind for me. The first is to assign layers to gameobjects and use Physics.IgnoreCollision() so that the layers that you don't want to set off triggers ignore each other. Another method you could use is to check the tag of the object that's causing the trigger. The OnTriggerEnter method actually has a parameter. You can write your OnTriggerEnter() method something like this:

     private void OnTriggerEnter(Collider other)
     {
         // if the object that's colliding/triggering doesn't have this tag, then run this code
         if (beingCarried && other.CompareTag("Your Tag") == false)
         {
             touchedWall = true;
         }
     }


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 FudgePackingPro · May 18, 2020 at 11:23 PM 1
Share

Using Physics.IgnoreCollision() worked perfectly for what I was looking for, thanks!

avatar image blinkafrootable · May 19, 2020 at 12:19 AM 0
Share

Awesome, I'm glad that worked! Another tip for the future is that, if you don't like hard-coding the Physics.IgnoreCollision() functions, Unity also lets you edit which layers can collide from within the Unity Editor. Just go to Edit -> Project Settings -> Physics and then look at the Layer Collision $$anonymous$$atrix with all the checkmarks. Uncheck any boxes that you don't want collisions with.

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

230 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 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 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

How to get a variable from a triggers script. 0 Answers

GetComponent(); returning null 1 Answer

Box collider on a moving object-shopping cart 3 Answers

OnTriggerEnter Script only working on duplicated object 1 Answer

OnTriggerEnter2D crashing editor 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