Why do my physics objects rotate randomly sometimes when reparented
Here's a video of it:
The axes are physics objects with a trigger collider on the blade. When the trigger hits the target, the axe switches to kinematic (to disable movement) and becomes a child of the target.
As you can see, sometimes the axe just works like it's meant to, but other times the axe rotates to some random angle.
Here's the code for the axe if you're curious:
void OnTriggerEnter(Collider col)
{
print(GetComponent<Rigidbody>().velocity.magnitude);
if (/*col.gameObject.tag == "Target" &&*/ GetComponent<Rigidbody>().velocity.magnitude > minimumStickSpeed)
{
transform.parent = null;
transform.parent = col.gameObject.transform;
transform.GetComponent<Rigidbody>().isKinematic = true;//.constraints = RigidbodyConstraints.FreezeAll;
}
}
There is no script on the target.
Any ideas on how to stop the errant rotating?
$$anonymous$$aybe you should do parenting in Update() or FixedUpdate()
Answer by hexagonius · Jan 15, 2016 at 11:55 AM
You could freeze the rotation on the x axis as long as they didn't hit anything, as it seems you want them to only spin forward.
OnCollisionEnter you could set the angularVelocity to 0, making sure the axe does not spin anymore.
good practice rule:
Do the GetComponent in Awake and save a reference instead of doing that 3 times in OnCollisionEnter. Does not really matter here, but with more objects that might start to get your performance down at some point...
Your answer
![](https://koobas.hobune.stream/wayback/20220612060300im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody is colliding terrain on isKinematic = false. call although meshes don't touch 0 Answers
Rotate single axis of object to nearest 90 degrees 1 Answer
How do I keep rigidbodies from pushing each other? 2 Answers
Player rotates spastically on all axes when camera is turned 0 Answers
How to check if kinematic rigidbody is overlapping anything in the scene? 0 Answers