- Home /
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;
}
}
}
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;
}
}
Using Physics.IgnoreCollision() worked perfectly for what I was looking for, thanks!
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
![](https://koobas.hobune.stream/wayback/20220612234655im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a better way to tell when enemies are dead? 2 Answers
Help with 2d TowerDefense Game 2 Answers
Setting up Jump trigger 0 Answers