Search code examples
c#unity-game-enginemathvectorgame-physics

Is there a way to check if a force is being applied to an object?


I have an object that can move dynamically in any direction. I want to pull it in a set direction, at a constant speed, but only if it's not moving in the general pull direction (not necessarily parallel). In other words, if it's moving towards me, I don't need to pull it.

My question then is; is there a way to check if this object is moving in this general pull direction?


Solution

  • If the dot product between the direction the "pull" force is applying and the current velocity is greater than 1, then the velocity is going in the direction of the pull.

    Vector3 pullDirection;
    Vector3 currentVelocity;
    
    bool isMovingInPullDirection = Vector3.Dot(pullDirection, currentVelocity) > 0;