Hi everyone,
Ok, here is the scenario: I am generating "ripples" at mouse click points in order to exert force on an object and move it around. I want to set it up so that if the ripple is generated close to the object (small radius when the ripple hits the object), more force is applied, and if the ripple hits the object close to the ripple's maximum outer radius, only a small force is applied.
The way I'm trying to do it is to generate a vector from the ripple's center to the object's center, generate a copy of that vector (to determine the correct direction) with the maximum magnitude possible, subtract the original vector from the maximum vector, and apply the difference. That way, if the ripple center and object are close together, the resulting vector will be large, and a large force applied, and if the ripple center and object are far apart, the resulting vector will be small, and a small force applied.
However, `Vector2.ClampMagnitude` only clamps down if the magnitude is greater than the maximum specified, and my original vectors will always be smaller than that. So I need a way to clamp the magnitude upwards to a minimum.
Here's my code so far:
function OnTriggerEnter (bubble : Collider)
{
Debug.Log("Hit " + bubble.gameObject.name);
var bubbleVect : Vector2 = bubble.transform.position;
var thisVect : Vector2 = transform.position;
var originalVect : Vector2 = bubbleVect - thisVect;
// ideally the next line would create a vector in the exact direction
// of originalVect but with magnitude maxVectLength
var maxVect : Vector2 = Vector2.ClampMagnitude(originalVect, maxVectLength);
var push : Vector2 = maxVect - originalVect;
bubble.SendMessage("RippleMove", push);
}
Any suggestions?
ETA:
Thanks for the answers. In the end I went with something along the lines of Peter G's answer:
var originalVect : Vector2 = bubbleVect - thisVect;
// find the direction
var maxVect : Vector2 = Vector2.Scale(originalVect, Vector2(1000, 1000));
// create a copy that is outrageously huge, big enough to have a magnitude
// greater than maxVectLength even if the originalVect is tiny, like 0.1
maxVect = Vector2.ClampMagnitude(maxVect, maxVectLength);
// clamp the huge vector down to what I want
var push : Vector2 = maxVect - originalVect;
// calculate the difference