Set speed depending on distance between objects?
I'm working on an orbital system, where the rotation speed is higher, the closer the object is to the center of its system. Does anyone know an effective method for executing this? Thank you in advance.
Answer by akillingbeck · Aug 25, 2017 at 02:12 PM
You could try something like this:
public float EvaluateSpeed(float distance)
{
const float TOP_SPEED = 1000.0f;
const float MAX_DISTANCE_FOR_EFFECT = 100.0f;
return AnimationCurve.Linear(0.0f, TOP_SPEED,MAX_DISTANCE_FOR_EFFECT, 0.0f).Evaluate(distance);
}
Obviously, you can replace AnimationCurve.Linear with a custom AnimationCurve if you like if this function is part of a mono behaviour:
public class SpeedEvaluater: MonoBehaviour
{
public AnimationCurve m_speedCurve;
public float EvaluateSpeed(float distance)
{
return m_speedCurve.Evaluate(distance);
}
}
In these examples, the distance you pass in is the X axis of the curve, which returns the corressponding y axis value. (Speed)
Does AnimationCurve require an animation? The rotations are 100% code-driven.
Nope, it's just a normal mathematical function. y = f(x) If you add AnimationCurve field to a $$anonymous$$onoBehaviour, you can see it in the inspector.
Thank you so much! With a bit of dividing, this worked wonderfully!
Your answer
Follow this Question
Related Questions
Vector3.Distance keeps growing 0 Answers
How limit the movement using distance 2 Answers
vector 3 to unity.transform 1 Answer
Vector3.Distance returning offset values due to down force interference. 1 Answer
Lerp to a fixed distance 0 Answers