Follow rigidbody center of mass
Hi, it's I while I'm stuck with this problem.
I'm creating an item picker system to let you pick up items, bring it with you, rotate etc...
What I need to do is let the item picked up following a transform point in word space, but not with item.transform.position but with its point of mass(basically an offset). Like this, if you move the point of mass, you move the item in the new position based on its centre of mass position.
I already did something working but I'm stuck in the updating continuously the point to new point.
itemTarget.position = Vector3.Lerp(itemTarget.position, raycastPoint.position - targetRb.centerOfMass, Time.deltaTime * followSpeed);
Where itemTarget is the gameobject picked up, raycastPoint is the transform set in the middle of the item and targetRb is the item's rigidbody.
Everything works fine, and when I change the centerOfMass the item picked up it still works fine.
The problem is when I rotate the item works in weird ways because centerOfMass is local. I tried to apply the word position without success.
itemTarget.position = Vector3.Lerp(itemTarget.position, raycastPoint.position - targetRb.worldCenterOfMass, Time.deltaTime * followSpeed);
I tried to update the new position for one frame, but I need to update it continuously.
itemTarget.position += raycastPoint.position - targetRb.worldCenterOfMass;
Answer by PixelFireXY · Nov 22, 2019 at 11:52 AM
Solved with:
newPosition = raycastPoint.transform.position - (targetRb.worldCenterOfMass - itemTarget.position);
and update the position with:
itemTarget.position = Vector3.MoveTowards(transform.position, newPosition, speed * Time.deltaTime);
It's spartan but for now it works.
Your answer
![](https://koobas.hobune.stream/wayback/20220612215118im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody.MovePosition doesn't look smooth 2 Answers
Question about having a Rigidbody follow another GameObject via forces. 1 Answer
troubles setting center of mass with rigid body in a car script 0 Answers
step 10 space shooter asteroid random rotation not working 4 Answers
Problem with Rigidbody.rotation and AddTorque() being together 0 Answers