- Home /
How come my boost comes to an abrupt stop?
I want my character to boost and stop slowly. So I enter a 'while' loop, and set it to keep going until the force with which he's propelled forward is 0, and lessen the force at the end of the loop. So I'd think he's goes forward a little less every frame, and creates the effect of a slow stop, but instead it's all one very quick, abrupt movement.
public int boosts = 3;
public int boostCount = 0;
public float boostSpeed = 15.0f;
public int boocount = 3;
CharacterController cont;
void Start ()
{
cont = GetComponent<CharacterController>();
}
void Update ()
{
if (Input.GetButtonDown ("Boost") && boocount < boosts)
{
Vector3 force = transform.TransformDirection (0, 0, 1) * boostSpeed * Time.deltaTime;
while (force.z > 0)
{
cont.Move (force);
force.z -= (2.0f * Time.deltaTime);
}
boocount ++;
}
Comment
Answer by ScroodgeM · Aug 05, 2012 at 12:51 PM
public int boosts = 3; public int boostCount = 0; public float boostSpeed = 15.0f; public int boocount = 3; public AnimationCurve boostEffect = new AnimationCurve() { keys = new Keyframe[] { new Keyframe(0f, 0f), new Keyframe(2f, 15f), new Keyframe(4f, 0f) } };
CharacterController cont;
bool boostEnabled;
float boostStartTime;
void Start()
{
cont = GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetButtonDown("Boost") && boocount < boosts)
{
boostEnabled = true;
boostStartTime = Time.time;
boocount++;
}
if (boostEnabled)
{
cont.Move(transform.forward * boostEffect.Evaluate(Time.time - boostStartTime));
}
}
Nice to see more people read into the AnimationCurves ;)
+1