#Physics2D.OverlapCircleAll not working correctly?

42 messages · Page 1 of 1 (latest)

strange pilot
#

So this was working and I don't really know why it isn't anymore. I've made lots of changes, and now it's like OverlapCircleAll doesn't seem to work?

[SyncVar(OnChange = nameof(GroundedChange))] protected bool isGrounded = false; // Set to true during OnCollisionEnter2D()

public override void GroundedChange(bool oldValue, bool newValue, bool asServer)
{
    if(newValue == true)
        calcBounceServer();
}

[ServerRpc(RequireOwnership=false)]
protected void calcBounceServer()
{
    Collider2D[] hitColliders = Physics2D.OverlapCircleAll(transform.position, circleCollider.radius);
    Debug.Log("Length: " + hitColliders.Length);

For whatever reason, I keep getting Length: 0 during runtime

#

And I'm getting the debugs basically exactly when it makes contact with the ground

sinful tulip
strange pilot
sinful tulip
strange pilot
#

or wait

sinful tulip
#

For example with a raycast: gameObject.scene.GetPhysicsScene().Raycast()

strange pilot
#

Forgive me, I'm trying to understand. So instead of Raycast(), I should be using OverlapCircle?

sinful tulip
#

Yep

#

Raycast was just an example :)

strange pilot
#

Ok, I think I'm close...

        Collider2D[] results;
        int length = gameObject.scene.GetPhysicsScene2D().OverlapCircle(transform.position, circleCollider.radius, results, Physics2D.DefaultRaycastLayers);

This doesn't like the use of results here? Use of unassigned local variable 'results'

#

I assume it probably wants me to set a size, but I don't really want to limit it? I'd prefer for it to be dynamic?

sinful tulip
strange pilot
sinful tulip
#

It's for performance :)

strange pilot
#

Ugh I dislike lists!

sinful tulip
#

Lol, why?

strange pilot
#

Idk I've not got much experience with them

#

I'm much more comfortable with arrays

#

But, I'll conform!

sinful tulip
#

I like lists, I'm working on my own todo list app in my spare time, but that's off topic xD

strange pilot
#

I need a todo list app....

#

Do I need to worry about using anything special for ContactFilter2D?

sinful tulip
#

I guess it depends on the situation.

#

You can use ContactFilter2D.NoFilter

strange pilot
#

But wasn't sure if there was anything special related to PhysicsScene2D

#
List<Collider2D> results = new List<Collider2D>();
int length = gameObject.scene.GetPhysicsScene2D().OverlapCircle(transform.position, circleCollider.radius, ContactFilter2D.NoFilter, results);

For some reason I can't get it to accept this definition?

#
Argument 3: cannot convert from 'method group' to 'Collider2D[]'
Argument 4: cannot convert from 'System.Collections.Generic.List<UnityEngine.Collider2D>' to 'int'```
sinful tulip
#
ContactFilter2D filter = new ContactFilter2D();
filter.NoFilter();
strange pilot
#

same errors :/

sinful tulip
#

Really? Like this?

List<Collider2D> results = new List<Collider2D>();
ContactFilter2D filter = new ContactFilter2D();
filter.NoFilter();
int length = gameObject.scene.GetPhysicsScene2D().OverlapCircle(transform.position, 4, filter, results);
strange pilot
#

Oops, I did it wrong, yes that works

#

I had filter.NoFilter in my function call, but the way you have it works!

#

Alright, finally I can test... lol

sinful tulip
#

🤞

strange pilot
#

I got numbers that are not zero now!

#

Time to rework my code to work with lists now

strange pilot
#

It works! Thanks again @sinful tulip