[SteamVR] Drum physics

Sorry if this is a rookie Unity thing to ask. Clearly I need some education on Unity physics. I’ve searched around but can’t seem to find what I’m looking for.

I’ve got a tracked object with capsule collider (tip of the drum stick) and rigidbody. I’ve got a drum surface which is basically a one-sided mesh I created with a mesh collider on it. The fact that this is SteamVR and tracked controllers may not even matter for this question, but that’s my case.

Naturally I want the drum surface to trigger and play a sound when struck from the top but not when struck from any other side. So in VR the controller does not physically impact against the surface so it will pass through the surface. So the surface gets triggered on the up stroke so I get a double beat effect, when I only want the down stroke.

I thought my one-sided mesh would mean that collisions/triggers would only happen when the controller capsule collider hits the surface from the top since the mesh it one sided, but I seem to get triggered from both sides.

I may end up writing some code to convert the capsule collider hit to the drum surface local space and compare, but I’d rather it just worked.

Any advice on how to set this up properly would be of tremendous help.

Thanks so much.

This is more of a suggestion than an answer, but you could check if your capsule collider has a negative-Y displacement in the collision event. This would ensure that the sound is only triggered when the collider is hit, while the capsule is moving downward.

Using the OnTriggerEnter event (i.e. this script has to be attached to your drum surface):

public class yourClass()
{
    bool movingDown;
    public GameObject yourCollider; //assign this in Unity
    Vector3 currentPosition;
    void Update()
    {
        if (yourCollider.transform.position != currentPosition)
        {
            if (position.y < currentPosition.y)
                movingDown = true;
            else
                movingDown = false;

            currentPosition = yourCollider.transform.position;
        }
    }
    void OnTriggerEnter()
    {
        if (movingDown)
        {
            //Play sound
        }
    }
}

With regards to specifics about VR, I probably wouldn’t be able to assist you very much, but this should give you a gist.

Check out this tutorial on scripting with colliders 3D Physics - Unity Learn

Thanks for the reply man. Can you elaborate? Is there a property in the collision event I’m missing or do you mean comparing the transform position of the drum stick tip collider with the the transform position of the drum surface?

The calculation would have to transform the capsule collider transform position into the local space of the drum surface because the drum will never be in a perfect vertical orientation in world space. I’m not sure what this calculation is but I believe its some form of drum surface.transform.TransformPoint(drum stick tip.transform.position)?

If I understand properly, you might just want to increase the size of your trigger collider so that when you hit it from top you are still in it when you go through the mesh of the drum.

If you want to avoid the user to play the drum from the wrong direction, you could have 2 triggers that condition each other, one at the top of the drum (A) and another at the bottom (B) (you can’t activate A if B has been activated already && you can’t play A again if you hit B).

I hope it helps!