- Home /
many collisions while holding an item
Hello all,
I'm sending a massage to the log file when I pick an Item with HTC Vive controller. in my log file I get this:
Picked item, name : AppleRed at: 6/25/2017 5:43:17 PM
Picked item, name : AppleRed at: 6/25/2017 5:43:17 PM
Picked item, name : AppleRed at: 6/25/2017 5:43:18 PM
Picked item, name : AppleRed at: 6/25/2017 5:43:18 PM
Picked item, name : AppleRed at: 6/25/2017 5:43:18 PM
I understand that it might be because it counts several collision per frame, while I hold the item with the controller, can this be fixed?
I attach the script for reference.
private void SetCollidingObject(Collider col)
{
if (collidingObject || !col.GetComponent<Rigidbody>())
{
return;
}
collidingObject = col.gameObject;
if (col.gameObject.tag == "Item")
{
LogManager.AddLine("\n\n" + "Picked item, name : " + col.gameObject.name);
}
}
Answer by EpsilonQoppa · Jun 25, 2017 at 11:42 PM
Maybe try something like this..
bool m_CollisionEnabled = true;
void OnCollisionEnter(Collision collision)
{
if (m_CollisionEnabled)
{
m_CollisionEnabled = false;
//Insert Functions Inbetween
StartCoroutine(EnableCollision());
}
}
IEnumerator EnableCollision()
{
yield return new WaitForEndOfFrame();
m_CollisionEnabled = true;
}
Now that I think about it, you may be better off waiting for half a second or so ins$$anonymous$$d of a frame because they could still be touching the next frame.
Ok, I've referenced he collider col in the IEnumerator, and it gives me an error.... UH!
collidingObject = col.gameObject;
if (m_collisionEnabled)
{
m_collisionEnabled = false;
StartCoroutine(EnableCollision(collider col)); //GOT ERROR HERE
}
}
IEnumarator(Collider col) //REFERENCED here
{
if (col.gameObject.tag == "Item")
{
yield return new WaitForSeconds(3);
col.gameObject.GetComponent<Rigidbody>().useGravity = true;
col.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = false;
m_CollisionEnabled = true;
}
}
I get it all the time. I try to add a variable within the function title and then not be able to reference that function...
Not sure it's a good fix @$$anonymous$$roq
because my lines use col.gameObject.GetComponent<>()
which is not known in the IEnumerator function.
private void SetCollidingObject(Collider col)
{
if (collidingObject || !col.GetComponent<Rigidbody>())
{
return;
}
collidingObject = col.gameObject;
if (m_collisionEnabled)
{
m_collisionEnabled = false;
}
if (col.gameObject.tag == "Item")
{
col.gameObject.GetComponent<Rigidbody>().useGravity = true;
col.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = false;
}