Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A Tricky Problem Force and Velocity

Discussion in 'Scripting' started by Corpse0327, Nov 15, 2014.

  1. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    I think everyone uses velocity in order to get if an object is applied force, right?

    The problem is after i apply force to a rigidbody and check its velocity. Unity says it is not moving. Yes it is not moving but it was applied force. For this delay my code thinks the object was applied force and stopped moving since it is not moving. This causes my code to run the code before the object starts moving while it should after the object stops.

    ExampleCode
    if(ObjectWasAppliedForce())
    {
    if(ObjectNotMoving())
    {
    ....
    ....​
    }​
    }

    Which i think is caused by "the force that is applied to a rigidbody takes effect after one frame" or "since physics and render updates are not synced it happens".

    How can i overcome this? I am really out of solutions. If i cant find any solutions, i will try a workaround, which is i hate to do.

    Thanks in advance.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Create a list of rigidbodies to which you applied a force, or maybe add a script to all rigidbodies where you can set a flag that a force was applied. After you "consumed" that information, you need to reset it.
     
  3. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Thanks Dantus. So basically there is no solution, only workarounds. :(
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That is not a workaround. Rigidbodies don't have have a "sensor" to detect when forces are applied. What you are looking for simply doesn't exist. When you apply a force, the velocity will be changed before the next FixedUpdate. But there is no way to find out when that force was applied. The proposed solution is the only reliable way I can think of.