- Home /
How to make a Shmup Graze mechanic
I'm making a bullet hell and I really wanna add a graze mechanic, however I can't figure out how to make the script only graze bullets once per bullet. I'm using Physics.OverlapSphere to get a list of the bullets near me, however it updates every frame, so it will return true for the same bullet multiple times if it has not yet left the radius of the OverlapSphere. any help would be greatly appreciated, and I can give more context if required, Thanks.
Answer by Eno-Khaon · Sep 28, 2021 at 04:02 AM
Rather than using Physics.OverlapSphere(), it might be more sensible to create a trigger collider around the player, then keep track of bullets that enter and leave its range using OnTriggerEnter() and OnTriggerExit().
Yeah the only issue with that is if I create a child object and put a collider on it, unity will treat that graze collider as the player's hitbox as well (since my parent has the player's hitbox), meaning that enemies only have to hit the graze collider instead of the smaller hitbox they're intended to hit. However you're completely right, I'll probably just have to make the parent a child and do it like you suggest.
Are you already using a trigger collider as the main "collision" detection, then?
If so, you could take this approach, then:
Attach *BOTH* trigger colliders to child objects of your main player (i.e. "grazing" trigger and "impact" trigger). Give each child a script consisting only of OnTriggerEnter() and OnTriggerExit() (as applicable), where all those functions need to do is send a message to a script on your main player GameObject.
You would be able to keep track of the results of those triggers like you already are, but where the two trigger colliders would activate independently.
For a very simple example:
// Script on main object:
public class ParentWithRigidbody : MonoBehaviour
{
public void MessageFromChildA(string message)
{
Debug.Log("ChildA trigger -> " + message);
}
public void MessageFromChildB(string message)
{
Debug.Log("ChildB trigger -> " + message);
}
}
// -------------------------------
// Script on Child A
public class ChildTriggerA : MonoBehaviour
{
public ParentWithRigidbody parentRB;
private void OnTriggerEnter(Collider other)
{
parentRB.MessageFromChildA(other.gameObject.name);
}
}
// -------------------------------
// Script on Child B
public class ChildTriggerB : MonoBehaviour
{
public ParentWithRigidbody parentRB;
private void OnTriggerEnter(Collider other)
{
parentRB.MessageFromChildB(other.gameObject.name);
}
}
Edit: Fixed a significant, silly, obvious, yet largely unimportant typo.
Oh my gosh that's genius I had never thought of doing things like that it's so simple and concise compared to what I was doing. I was dealing with a bunch of AI stuff recently that had me thinking I had to use OverlapSphere or CheckSphere to find other collisions. Thanks a bunch! It's really nice to have met a stranger with so much dedication to helping me :D
Your answer