2
\$\begingroup\$

This may actually be more of a Physics.SE question but since im doing this in Unity I figured I'd ask here.

I am playing around with the Constant Force component and have a sphere constrained with a linear configurable joint (y-axis free). With one key I apply a force in one direction to get it moving, but then when i press another key, I want to stop it and hold exactly in place by using an opposite force. So even if some other rigid body hits the sphere, it wont move until I remove a force.

Unfortunately, Unity doesnt allow more than one constant force on a gameobject (probably a good reason, not sure exactly) and I know if I do a net force, that it would just end up being 0, which would kind of work but the sphere would move if it gets hit.

Another thing I tried was to take the spheres current force and constantly flip it from positive to negative every FixedUpdate but that didn't seem to work at all.

I know the quick and easy way would be to just set velocity to zero, but I see tons of comments saying that modifying velocity directly breaks the physics, is bad form, etc. and I want to do this as properly as possible.

The best way I can describe what I'm trying to do is like that science experiment where you take a ping-pong ball and use a blower to push just enough air to counter gravity and the ball seems to hover in place. My only difference is that my constraint is side to side instead of up/down.

\$\endgroup\$
  • \$\begingroup\$ I'm not a unity user but can you not add a fixed point constraint and turn it on and off with keypresses? Or change it from a dynamic body to a static one? \$\endgroup\$ – Steven Jan 6 '16 at 3:06
  • \$\begingroup\$ any two constants add to a third constant - so constraint of a single constant force is eminently sensible. \$\endgroup\$ – Pieter Geerkens Jan 6 '16 at 3:14
  • \$\begingroup\$ I see this question bounce back up from time to time and it always kind of baffles me. Applying a constant force to an object will accelerate it over time, not hold it in one place. If you have an object moving due to force A in one direction, applying force -A will neutralise the acceleration but not hold it in place. You'd need to increase the counter force to decelerate the object, then decrease it again to a resting force. Responding to an impact would require similar dynamic adjustments. It doesn't look like a constant force is the right tool for this job. \$\endgroup\$ – DMGregory Oct 22 '17 at 11:39
  • \$\begingroup\$ Maybe this question is really about a spring/damping constraint? In this situation, as an object is displaced from its equilibrium position, it experiences an escalating force in the direction opposite the deviation, until it stops or reverses the movement and returns to equilibrium. Unlike a constant force, this responds dynamically in proportion to other physics effects on the object. If this question is still of interest, would this type of spring/damper setup make for a suitable answer? \$\endgroup\$ – DMGregory Oct 22 '17 at 12:20
1
\$\begingroup\$

In your case I wouldn't use a force to get it moving at all, but instead set the position / velocity directly on response to the key presses, and mark the object as kinematic.

What you're describing is fundamentally not a natural movement for the sphere. You're talking about applying unrealistic forces out of nowhere to get it into a specific position/velocity. That's what kinematic objects are for - to be able to set those properties directly and then have the other objects in the simulation react to their unnatural movement.

Moving objects only by applying forces to them is great, when you want even the initiating object to move naturally (e.g. a cue ball on a pool table reacting to a non-existent cue that has struck it); but it's not required. It is common to have a single kinematic object, directly controlled by the player/user, which is causing interesting things to happy in your simulation. You may have problems when you try to have more than one kinematic object, or try to move it in a way that is so unrealistic it is beyond the ability of the physical simulation to cope, but if you are careful about how you move it, you're fine.

\$\endgroup\$
0
\$\begingroup\$

According to your needs there is only one thing is left, that is Is Kinematic attribute of Rigidbody. Try to turn it on when you want to stop your body.

\$\endgroup\$
0
\$\begingroup\$

There's two things you can try:
- Set it to Is Kinematic, so it will not move unless you explicit do it by code.
- Set the collision object as IsTrigger, so it will not move if another object collide with it, and to maintain the object on the air, try this:

float massObject = 1; // adjust for the crate mass, if needed
Rigidbody rb; //to get the rigidbody component

void Start () 
{
    rb = GetComponent<Rigidbody>();
}

function FixedUpdate()
{ 
      rb.AddForce(-Physics.gravity * (rb.mass+massCrate)); 
}
\$\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.