- Home /
IngoreCollision() does not work
I know this is a really basic question, but it is stumping me and I can't find an answer online anywhere.
So I have an item -- just a cube with a box collider and rigidbody reshaped to look like a tablet with some writing on it -- that I want to 'pick up.' I put a sphere collider on the character to detect when I'm near the object via triggers. When I do, I then put the item into the character's hand. Here is the entire script in question:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoldItem : MonoBehaviour
{
public GameObject item=null;
private Animator heroAnim;
private Transform objectSpot;
private void Awake()
{
heroAnim = GetComponent<Animator>();
objectSpot = this.transform.Find("mixamorig:Hips/mixamorig:Spine/mixamorig:Spine1/mixamorig:Spine2/mixamorig:RightShoulder/mixamorig:RightArm/mixamorig:RightForeArm/mixamorig:RightHand/ObjectSpot");
if (item != null)
ReceiveItem(item, true);
}
private void ReceiveItem(GameObject newItem, bool force = false)
{
if (force || item == null)
{
Rigidbody rb = newItem.GetComponent<Rigidbody>();
if (rb == null)
Debug.Log("Item has no rigidbody.");
Physics.IgnoreCollision(newItem.GetComponent<Collider>(), gameObject.GetComponent<Collider>());
//Physics.IgnoreLayerCollision(8, 9);
rb.isKinematic = true;
rb.transform.SetParent(objectSpot);
rb.transform.localPosition = Vector3.zero;
rb.gameObject.transform.localPosition = Vector3.zero;
item = newItem;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Holdable Item")
ReceiveItem(other.gameObject);
}
}
When I do this, the cube object is still asserting physics on the character and the character starts to slide away from the cube held in his hand. I am calling to ignore the collision, so I don't know what this would be. I've tried also grabbing the trigger sphere collider and ignore it and that doesn't help.
However, note the commented-out IgnoreLayerCollision() call. It works just fine if I use it. But I really wanted to specifically turn off physics collisions for the character and the object in his hand, not for the whole layer.
Your answer
Follow this Question
Related Questions
ignorecollision return error 0 Answers
How to stop an Interactive Cloth colliding with box collider 1 Answer
Physics.IgnoreCollision on layers 0 Answers
How do I call OnEnable on a CharacterController? 1 Answer
Rigidbody Collision Question 1 Answer