#Is it a bug that caching rigidbody on the parent causes bones to break

1 messages · Page 1 of 1 (latest)

pure bluff
#

I just went down a rabbit hole on the rigidbody component. My bones on a character rig have rigidbody components, but for this to work I can't have any on the parent gameObjects as you can only have a single chain of rigidbody on a gameObject. Long story short I narrowed it down to me caching the rigidbody components on Awake for my Death class to deal with ragdolls, this was on my parent gameObject. Is this a bug in Unity? It sent me on a wild goose chase thats for sure.

alpine pier
#

Saving a reference to a Rigidbody in a variable is not going to cause any behavioral changes. I'm certain you're mistaken

#

More likely whatever you did with that variable after storing the reference is the issue

alpine pier
#

You'd have to show the code before and after to be sure of what you're talking about

pure bluff
#

Now that I look at it again I wonder if its because after caching the rigidbody I run a method to initialize their state, even though its false.

You can see in my changes I moved the get from an awake cache to happen on death, I also make sure to clear in the case of resurrection.

#

Also a note I also didn't mention. I disabled all my scripts on my root GameObject and it still caused the issue, which is bizzare because I was under the assumption the scripts wouldn't run at all when disabled. But I kid you not it was reproduceable by deleting the Death class, even when it was already disabled.

alpine pier
#

TBH though from your description I'm not exactly sure what issue you were running into

#

It's not clear what was behaving differently than expected

pure bluff
#

Its my understanding you cannot have multiple rigidbody components in a hierarchy in Unity. I was lead to believe the way it works on bones on characters is on the back end the hierarchy doesn't exist and it treats all of the individual rigidbody as a single chain. So with a character for instance if you have rigidbody components on your bones for ragdoll, you can't also have have a rigidbody on the parent gameObject node. The actual issue I was having was when you test collision from raycasts it doesn't work, in my case my projectiles where not hitting my enemies.

As an example in my testing if I took my character model and rig out of my parent gameObject it started to work, but as soon as I put it back in it stopped working. The only thing on my gameObject parent where my code classes which is what lead me down the rabbit hole. Just to be clear, I didn't have a rigidbody component on my parent gameObject, but I did have a cache of one in my code on Awake. Additionally for whatever reason even when this code class was disabled it was still breaking my Rigidbody raycasts which was extremely odd.

alpine pier
#

Can you be more specific about what "working" and "not working" means in this context?

pure bluff
#

Raycasts fail to hit the colliders was the fail I was seeing.

alpine pier
#

Two things:

  • raycasts will not hit disabled colliders
  • raycasts will not hit colliders that they start inside
pure bluff
#

Apparently it does based on what I experienced.

alpine pier
#

I suspect something else was at play, but i'd have to see the full script to be sure

#

There are also often timing issues with moving colliders and raycasts - colliders essentially don't fully move in the physics scene until a physics simulation step has happened or you call Physics.SyncTransforms()

pure bluff
#

I will try and get a proper repro tonight, but I was screaming in my head with this problem for about 3-4 hours. It made absolutely no sense to me.

#

The collider would have had a least a few frames to be tested for collision. When I instantiate my projectile I translate it based on the frame time and raycast that distance and add in some buffer. I have never had issues hitting thin collision. Even if it missed one frame the bullet isn't instant so it probably has a good few frames that it would get a valid hit at high frame rates when you account for the buffer.

#

Like I said before as soon as I deleted my scripts it started working, I had my two characters right next to each other and they where identical, one with this death script and the other without and one had valid collision and the other didn't.

#

It was extremely odd which is what made me think it was a bug in Unity itself.

#

That or I really got confused on something, but I would say I was 90% confident in my repro, I'm just doubting myself hearing it shouldn't be causing an issue.

alpine pier
#

It's just that... there's no way that simply storing a reference in a variable has any effect on the physics engine. It's completely isolated

#

the C# runtime and the physics engine are completely separate things.

pure bluff
#

Oh wait... I'm a plank. I just noticed I had rb.detectCollisions getting set to false.

#

FML

#

well this was a massive waste of time 🤦‍♂️

alpine pier
#

Yuuup that'll do it

pure bluff
#

I apogolize for wasting your time, so embarrassing. It was right infront of me all along. I got so caught up in my heirchy being wrong as I changed it from a single collider to full body collision.