Rigibody constraints do not work. Still moves a little.
I have root object with rigibody only and with childrens with colliders. Such a hierarchy is due to the fact that root is spawn point for spawn objects and used to rotate them.
Rigidbody(root)
|-children(collider)
|-children2(collider)
...
The problem is that root rigidbody with constraints (allowed only Y rotation) slightly shifted with each collision of his children with other obstacles. If "Is Kinematic == true" - all ok, coordinates are stable. But I need to rotate my root rigidbody with physics, to stop it if it children ran into obstacles.
Please help me. I have no idea why the restrictions do not work.
Answer by Mikhail94 · Mar 24, 2018 at 04:17 PM
Solution after several hours of heavy searching and debugging.
private void Start()
{
// \ (•◡•) /
Rigidbody rb = target.GetComponent<Rigidbody>();
rb.centerOfMass = Vector3.zero;
rb.inertiaTensorRotation = Quaternion.identity;
}
For details read
https://docs.unity3d.com/ScriptReference/Rigidbody-centerOfMass.html https://docs.unity3d.com/ScriptReference/Rigidbody-inertiaTensorRotation.html
Thank you so much! I hope this solves the problems I've been having, I've been trying to use Collision events ins$$anonymous$$d of Trigger events for my hitboxes and hurtboxes used for combat in my game, and the rigidbodies have been wiggling out of whack >.>
[EDIT] - While this wasn't my particular problem, it was interesting.. Not sure if it helped, but wow.. it turns out my collider wasn't big enough for the sword swiping in the animation.. Lol T_T
Thanks for this. Something changed and all of the sudden, my constraints weren't working. Now this is. I'll add a TODO to see if I can find it out in the future.
This was exactly what I needed to fix the issue. I encountered the issue while trying to implement pushable boxes in a level for a game. In case anyone wants to see a real world example of how this problem manifests, I made a YouTube tutorial demonstrating it here: https://www.youtube.com/watch?v=maIAiSRBEdE
Thank you! This saved me from running a rotation check and correction on every frame! Your hard work is helping me and others!! #communitySupport :-D
Answer by ristophonics · May 27, 2019 at 04:24 PM
hmm this has not worked for me.
I was used a hinge joint to create a steering wheel assets that was working very well in Unity 5.6. I imported the asset to 2018.4 with the same configuration but the constraints no longer function as they did in 5.6. I used the above code and several variations to get it to properly constrain position and select rotations but none worked. Not sure what to do...
Answer by RHPOSITIVE · Jul 30, 2019 at 02:28 PM
I tried many things.. (I've also tried the recommendations here.) It didn't work. So, I write simple code. I know; It's a really UGLY method, but absolutely working.. Just attach to dynamic object.
public class PositionCorrection : MonoBehaviour
{
GameObject positionHelper;
WaitForSeconds phDuration = new WaitForSeconds(1f);
void Start()
{
GameObject positionHelperParent = GameObject.Find("Position Helper Parent");
if (positionHelperParent == null)
{
positionHelperParent = new GameObject("Position Helper Parent");
}
positionHelper = new GameObject("P.H. " + gameObject.name);
positionHelper.transform.SetParent(positionHelperParent.transform);
StartCoroutine(PositionFix());
}
IEnumerator PositionFix()
{
while (true)
{
if ((positionHelper.transform.position - transform.position).sqrMagnitude < .01f)
{
transform.position = positionHelper.transform.position;
}
else
{
positionHelper.transform.position = transform.position;
}
yield return phDuration;
}
}
}
Answer by onurkiris05 · Apr 20 at 11:06 AM
I had the same issue with my enemies that using NavMeshAgent . Current solution in the 'Best Answer' didnt work for me.
Well after some digging into game, i found out that NavMeshAgent overrides transform.position so upon enemy death, adding this line 'navMeshAgent.enabled=false;' solved my problem.
Your answer
Follow this Question
Related Questions
Objects slowly move by their own? 1 Answer
AddRigidBody Script Error 1 Answer