#Collisions being detected more than once

1 messages · Page 1 of 1 (latest)

elder steppe
#

image is of the enemy

#

messed up part of this whole collision system is, that when I manually move the enemies, it works fine, but only when I use spline animate.Play() to move them, do they trigger these collisions

dapper adder
#

What does your code look like?

elder steppe
#

I will try to just move the enemies using addforce or smth and check for results then, I am too tired rn, will do it later

dapper adder
elder steppe
dapper adder
#

It will also run for every different collider interacted with.

#

You should be logging which colliders are actually involved, etc

#

For example if two objectws which each have two colliders and each have OnTriggerEnter2D on them intersect, you'll get 4 calls

elder steppe
#

I did, it's the same damn collider which keeps popping up...I used debugging too

dapper adder
#

Have you tried adding OnTriggerExit2D as well

#

and logging that?

#

To see if it thinks the collider is exiting

elder steppe
#

oh...nope

#

what would that achieve though

dapper adder
#

One cannot fix a problem one does not understand

elder steppe
dapper adder
elder steppe
dapper adder
#

Can you show what the duck looks like

#

what components it has and any children etc

#

look at this

#

do you see the green circle on the left?

#

isn't that the collider?

elder steppe
dapper adder
#

Is your collider way offset from your poop object?

elder steppe
# dapper adder isn't that the collider?

that is the instance of the gameobject which i use as a reference for pooling, it is supposed to be outside the game view and not to be used as an actual unit

dapper adder
elder steppe
# dapper adder 1. Why is it active? 2. Why don't you just use a prefab? 3. Isn't it possible th...

well, i started out without using prefabs, and ultimately reached a point where if I stopped using these instances, i would have to rewrite a lot of code, which i don't rly want to since it's working for the time being. As for this thing causing the issue, it's impossible, since its very out of range and the debug only pops up, when the duck collides with the duck poop unit in the game screen, and not the one offscreen

#

and i keep them active so that i don't have to deactivate and active them back and forth while changing stuff and checking stuff like range colliders and sprite renderers out

dapper adder
# elder steppe well, i started out *without* using prefabs, and ultimately reached a point wher...

As for this thing causing the issue, it's impossible, since its very out of range and the debug only pops up, when the duck collides with the duck poop unit in the game screen, and not the one offscreen
We could rule it out with complete confidence if you print out the instance id of the object and/or use Debug.Log passing itself as the context and seeing which one it highlights when you click the log line

elder steppe
#

you can do that?

dapper adder
#

you can print anything you want

#

instance id is a good debugging tool

elder steppe
dapper adder
#

Debug.Log prints whatever you give it

#

so if you pass in the instance id, it will print that

elder steppe
#

as in, instances of the same gameobject have the same name, so i always wondered how you would know which of the instances is triggerring something

dapper adder
#

yes, instance id

dapper adder
#

can you show that code that produced this?

dapper adder
#

not of the fart thing

#

And you can just combine these logs into one line:

Debug.Log($"{collision.tag} Exited Trigger. My Instance id is {GetInstanceId()}. The collided object instance id is {collision.GetInstanceId()}");```
#

btw if you put your inspector into debug mode you'll be able to see the instance IDs of GameObjects and components in the inspector

#

(or actually you're printing the instance ID of the duck's collider)

#

but this is good - we now know it's only one collider on the duck's side that is interacting

#

Once you print the fart's instance ID too we'll get a more complete picture

elder steppe
elder steppe
#

it's named Fartoof(), because once the duck is in range of the poop, the poop ends up farting and this script is what sets the fart off

dapper adder
#

that's why collision.GetInstanceID() gives us the duck's ID, because collision is the other object.

elder steppe
#

OH!

dapper adder
#

GetInstanceId() without anything will give us the poop's Fartoof script's instance ID

dapper adder
#

Use this:

Debug.Log($"{collision.tag} Exited Trigger. My Instance id is {GetInstanceId()}. The collided object instance id is {collision.GetInstanceId()}");```
It will be so much clearer
elder steppe
elder steppe
dapper adder
#

wdym "didn't workk"?

#

Also you'd want a version sayhing "entered trigger" in OnTriggerEnter2D

#

the exit one is for... exit ofc

elder steppe
dapper adder
#

Are you saying there was a compile error?

elder steppe
#

nvm i will give you the new debug log

dapper adder
#

It's probably just because I wrote GetInstanceId instead of GetInstanceID with the capital D

elder steppe
#

OHHHHHH

#

i did not see that

#

this is pretty weird

#

i cross checked the instance ids, and all of them are matching

dapper adder
#

ok so we can at least rule out the "there's a second poop object" involved theory

elder steppe
#

just to remind you, if i were to manually move the duck, it would do the logs and collision only once, like it's supposed to

#

when i was using spline animate, at a higher speed, it did not do the same thing, too many collisions were being detected

#

now that i am altering the time field, it's logs became less, but they still show the same behaviour

dapper adder
#

I'm not sure if you've actually showed the spline animate inspector or anything yet

dapper adder
#

Let's turn off Debug mode in the inspector 😛

dapper adder
# elder steppe

wait a sec I also just noticed someting... Your poop object has a Rigidbody2D on the root and a separate one on the child object "InRangeCollider"?

#

Why is that?

#

(actually not clear why it has a Rigidbody at all tbh)

dapper adder
# elder steppe

Also if your duck is moving via SplineAnimate it should probably have its Rigidbody2D set to kinematic, not dynamic

elder steppe
dapper adder
#

Well - yes you need a rigidbody

#

but I said to make it kinematic

#

not to remove it

dapper adder
#

and as for the pooop

#

only one object in the hiearchy needs one

#

ALl colliders on all child objects are part of the top level Rigidbody

elder steppe
#

wait, so i can get rid of all the rigidbodies in the units and just keep one in the duck

dapper adder
#

But uh - yeah the poop needs 1 at most and since its not the moving one probably none at all

#

I would keep a rigidbody on any object that you expect to be moving during gameplay

#

otherwise it doesn't need one

elder steppe
#

💀

dapper adder
#

I do wonder if the double rigidbody is the cause here though I think it shouldn't be...

elder steppe
#

here's the thing though, i have another script which works with moving the units using the mouse, in that scenario, would i need a rb?

dapper adder
dapper adder
#

then no

dapper adder
#

did that fix it?

elder steppe
#

oh yeah it def did

dapper adder
#

😮‍💨 add it to the never ending list of weird-ass physics quirks in Unity

elder steppe
#

i srsly love u, i had been so bummed out cus i had been stuck on this for a long time