I am trying to implement a keyboard in unity. Therefore, I’ve created a key prefab with a box collider and no rigidbody and a circle with spherecollider and rigidbody. I am using the circle to represent a finger typing on the keys.
The problem I am facing is that the OnCollisionEnter(Collission collission)
method in the KeyCollissionHandler.cs script gets called even if the colliders of circle and key are not even close.
To visualize the problem take a look at the following screenshots.
Here you can see the pressed keys in the game scene, the grey & yellow keys are collided with the finger and the white ones are not collided.
Playmode with triggered colliders
Of course first I checked the size of the colliders and set them to a very small size of 0.5x0.5x0.5 for the key box colliders and 0.1 radius for the circle sphere collider. The gameobjects of key and circle are also relativly small (key: 0.135x0.08x0.15, circle: 0.01x0.01x0.01).
Summary: Colliders collide even if there is no gameobject near to it. Why does this happen?
Please just ask if you need more info, I will provide the informations you need asap.
KeyCollissionHandler.cs attached to all keys:
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using System;
using UnityEngine;
public class KeyCollisionHandler : MonoBehaviour
{
public string keyLetter; //Holds the letter of the actual key
public bool editorConfig = false; //For debugging purposes
private void Awake()
{
//Some awake stuff
}
private void OnCollisionEnter(Collision collision)
{
if (EventManager.instance != null)
{
EventManager.instance.OnAddCollission(this);
}
}
private void OnCollisionExit(Collision collision)
{
if (EventManager.instance != null)
{
EventManager.instance.OnRemoveCollission(this);
}
}
}
All the script does is call other actions to handle the key input into a textmesh and the colloring you saw in the first screenshot.
Unity Inspector Screenshot of the circle
Unity Inspector of the Key “R”
Collider of Key “R” which is triggered but should not be
Cheers