#Collisions being detected more than once
1 messages · Page 1 of 1 (latest)
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
like this
What does your code look like?
It's just a simple ontriggerenter2d with a long in it, nothing else
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
OnTriggerEnter2D will run one time per collider
It should, but as the other guy suggested, I will not use spline animate and check whether it works or not
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
I did, it's the same damn collider which keeps popping up...I used debugging too
Have you tried adding OnTriggerExit2D as well
and logging that?
To see if it thinks the collider is exiting
It would give you a clue as to what's happening
One cannot fix a problem one does not understand
I happened to change how the duck was moving across the spline by changing from using splineanimate to simple adding to the elapsedtime, and to a certain extent, the issue seems to have lessened, but it's still not working out
Can you type this into the search bar at the top of the hierarchy window?
t:Fartoof
Can you show what the duck looks like
what components it has and any children etc
Wait
look at this
do you see the green circle on the left?
isn't that the collider?
Is your collider way offset from your poop object?
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
nope
- Why is it active?
- Why don't you just use a prefab?
- Isn't it possible this "pooled" version is the thing causing this issue?
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
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
print out the instance id...?
you can do that?
i never knew you could print out different instance of the same object
Debug.Log prints whatever you give it
so if you pass in the instance id, it will print that
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
yes, instance id
can you show that code that produced this?
you're printing the instance id of the duck
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
the poop's to be honest, lel
wait, this was on the poop's inRangeCollider child object, not the duck
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
I know that
that's why collision.GetInstanceID() gives us the duck's ID, because collision is the other object.
OH!
GetInstanceId() without anything will give us the poop's Fartoof script's instance ID
see now I have no idea what you're printing
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
wait
i copy pasted this, and it didn't workk :/
wdym "didn't workk"?
Also you'd want a version sayhing "entered trigger" in OnTriggerEnter2D
the exit one is for... exit ofc
I had to overwrite it, so that the blue turned to yellow or smth
I have no idea what that means
Are you saying there was a compile error?
nvm i will give you the new debug log
It's probably just because I wrote GetInstanceId instead of GetInstanceID with the capital D
OHHHHHH
i did not see that
this is pretty weird
i cross checked the instance ids, and all of them are matching
ok so we can at least rule out the "there's a second poop object" involved theory
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
I'm not sure if you've actually showed the spline animate inspector or anything yet
Let's turn off Debug mode in the inspector 😛
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)
Also if your duck is moving via SplineAnimate it should probably have its Rigidbody2D set to kinematic, not dynamic
isn't rigidbody used to detect collisions? it's not like if i just added colliders, ontrigger enter would work
no
Well - yes you need a rigidbody
but I said to make it kinematic
not to remove it
Only one of the objects in the collisionneeds a Rigidbody2D
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
wait, so i can get rid of all the rigidbodies in the units and just keep one in the duck
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
💀
I do wonder if the double rigidbody is the cause here though I think it shouldn't be...
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?
DO they need to be detecting collisions with stuff while you're moving the mouse?
no...
then no
well damn
did that fix it?
😮💨 add it to the never ending list of weird-ass physics quirks in Unity
i srsly love u, i had been so bummed out cus i had been stuck on this for a long time