0
\$\begingroup\$

I am making a VR data visualisation app in Unity and I have a parent with many children (just some primitive cubes) that I can toss around the scene by just setting the parent's rigidbody to the same velocity as my tracked VR controller.

Rigidbody rb = GetComponent<Rigidbody>();
Vector3 controllerVelocity = Player.instance.rightHand.GetTrackedObjectVelocity(); // how I ref the controller velocity in SteamVR
rb.velocity = controllerVelocity * 2f; // the 2f is just to speed up the velocity 

The above code works fine, but the problem is I think the scale of the children objects, which can be adjusted by the player, is affecting the velocity the parent moves at. Or maybe just when the children are very large...the controller velocity is comparatively too slow? Basically I need this to not be the case; I want the parent's rigidbody to move at roughly the same velocity no matter the children's scales/masses.

So to achieve that I thought to use Rigidbody.AddForce but it doesn't seem to be making any difference i.e. larger children are still moving slower. Here is what I have so far:

Rigidbody rb = GetComponent<Rigidbody>();
Vector3 controllerVelocity = Player.instance.rightHand.GetTrackedObjectVelocity();
rb.AddForce(controllerVelocity  * 2f, ForceMode.VelocityChange);

I also tried ForceMode.Acceleration but then nothing moved at all? Am I using AddForce incorrectly? Or do I just need a scaling multiplier based on the size of the children? Any help is welcome.

\$\endgroup\$
4
  • \$\begingroup\$ I notice you're applying an absolute velocity as though it were a delta-V. Is it possible the previous velocity of the body is interfering in some way? Normally when I use this pattern, I compute deltaV = targetVelocity - rb.velocity; rb.AddForce(deltaV, ForceMode.VelocityChange); Or just set rb.velocity = targetVelocity directly. \$\endgroup\$
    – DMGregory
    Feb 17 at 19:31
  • \$\begingroup\$ So I tried what you suggested but the scale of the children is still affecting the velocity? Big children move slowly, small children move fast. \$\endgroup\$
    – AzzyDude
    Feb 17 at 19:52
  • \$\begingroup\$ I have also removed all children colliders to see if there is any change and there is also no difference. I am starting to think the velocity is the same for small and large children...it's just a physical fact that they have to move a greater distance when they're larger so the same velocity looks slower...? \$\endgroup\$
    – AzzyDude
    Feb 18 at 11:18
  • \$\begingroup\$ You could record some numbers to put that theory to the test. \$\endgroup\$
    – DMGregory
    Feb 18 at 11:41
0
\$\begingroup\$

Not ideal, but was able to mimic my intended result by doing the following:

float parentMagnitude = transform.lossyScale.magnitude;
float thrustAdjuster = parentMagnitude * 5f;
Vector3 controllerVelocity = Player.instance.rightHand.GetTrackedObjectVelocity();
rb.velocity = controllerVelocity * thrustAdjuster;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.