Hi,
I’m currently developing my first project which I actually plan on finishing It’s a sidescroller, where a bird can be shifted between 3 lanes (upper, middle, lower) to collect pickups for points. As I aim for 30fps (older Android phones) and the bird can get pretty fast, I ran into the issue where the bird skipped through the pickups and the collider wouldn’t trigger. I resolved this with a linecast from its current position to its position in the prior frame; this works good enough.
Now I wanted to add a “combo” feature, where a bonus is rewarded whenever 10 pickups are being collected without missing one. For this I have a combo counter with a reset whenever a pickup is being missed. To achieve this, I added two colliders - one above and one below the bird. I left the mesh renderer on in the pic for clarity. Now the colliders have the same issue of skipping through the pickups, but I cannot resolve it with a linecast, as pickups are not being detected when the bird is in the progress of switching lanes and only halfway there (the linecast is just a point and not as wide as the shown collider). Next, I tried a spherecast of the size of the whole “collider”:
private Vector3 lastPosition;
private RaycastHit hit;
[SerializeField]
private LayerMask collisionLayerPickups;
private float rayRadius;
private void Start()
{
rayRadius = transform.localScale.y / 2;
lastPosition = transform.position;
}
private void Update()
{
float rayLength = Vector3.Distance(transform.position, lastPosition);
Vector3 rayDirection = (lastPosition - transform.position).normalized;
if (Physics.SphereCast(transform.position + new Vector3(rayRadius, 0, 0), rayRadius, rayDirection, out hit, rayLength, collisionLayerPickups))
{
if (hit.transform.tag == "Pickup")
{
GameplayManager.Instance.ComboCounter(0, true);
}
}
lastPosition = transform.position;
}
However, this is also not working consistently. Some pickups are not recognised, and sometimes it seemingly randomly detects pickups where there are none.
As a last idea, I could add a huge collider behind the bird, big enough that no pickups can skip through it; however, this seems like a dirty solution, as it’s not very systematic (if the birds gets even faster, eventually things could slip through).
I hope you understand my issue. How would one solve this in a clean and tidy way? I am wondering the same for bullets in shooters, etc.
Thanks for your help!