I have an Transform approaching a target Object.
I want this transform’s speed to be reduced to zero in a logarithmic manner, the closer it gets to that object.
Any idea how I could do that?
I have an Transform approaching a target Object.
I want this transform’s speed to be reduced to zero in a logarithmic manner, the closer it gets to that object.
Any idea how I could do that?
The approach I usually take for things like this is to map the range of values I want into a range from 0 to 1. That value gets plugged into my function to produce the value I want. This is the same concept used by Lerp and similar functions.
To map your distance to the 0-1 range you could do this…
float maxDistance = 100.0f; // the range at which you want to start slowing
float percentageOfMax = Vector3.Distance(moving_object, target_object) / maxDistance;
// clamp the value to 0-1 so we don't have to do a comparison
percentageOfMax = Mathf.Clamp01(percentageOfMax);
// if you were using lerp to change the speed...
float speed = Mathf.Lerp(min_speed, max_speed, percentageOfMax);
You would just need to replace Lerp with your logarithmic interpolation function. That’s left as an exercise for the reader 'cuz I’d have to google for that.