# Huge velocity increase glitch

Hello! I’m making a sonic-like game, i have encountered a strange “glitch” (i don’t know if it’s really one) : my character (capsule collider + rigidbody + custom controller script) suddenly get an very high velocity.

What my controller do during this happen is running, adapting to the ground’s rotation an sticking to the ground (so the controller is able to run on ceilings).

Here is the code of the functions implied :

``````//The function that keeps the character aligned and sticked to the ground
public void AdaptAndStickGround(bool align = true, bool stick = true) {
//If the character isn't alligned with ground normal, do it
if (align && transform.up != groundHit.normal)
RotateTransformLerp_ToNormal(groundHit.normal, 2.5f);
//Stick to the ground if fast enough
if (stick && speedPerOne >= characterInfo.stickToGroud_minSpeed)
{
Vector3 localVelocity = directionTransform.InverseTransformDirection(_rigidbody.velocity);
Vector3 stickForce = -transform.up * Mathf.Clamp01(groundHit.distance - 1) * Mathf.Clamp01(localVelocity.z / characterInfo.maxSpeed) * characterInfo.stickToGround_force;

Debug.Log(_rigidbody.velocity + "  |   " + stickForce);
}
}

//What the "RotateTransformLerp_ToNormal" function used before leads to
private IEnumerator RotateTransformLerpCoroutine(Quaternion rotation, float speed = 1)
{
Quaternion baseRotation = transform.rotation;
float lerp = 0;
while (lerp < 1)
{
lerp += Time.deltaTime * speed;
transform.rotation = Quaternion.Slerp(baseRotation, rotation, lerp);
yield return new WaitForEndOfFrame();
}
}
``````

And here is a video of what happen : imgrush.com - This website is for sale! - imgrush Resources and Information.

You can see in the debug console the result of this line of code :

``````Debug.Log(_rigidbody.velocity + "  |   " + stickForce);
``````

I hope some of you will find how this happen, thanks for reading.

I have absolutly no idea why this happen.
The solution i found to fix it is to put a velocity limiter.

``````    public void LimitVelocity(float maxSpeedCoef = 1, float decelSpeed = 20, bool horizontalLimiterOnly = true, bool smooth = false)
{
Vector3 localVelocity = directionTransform.InverseTransformDirection(_rigidbody.velocity);
Vector3 force = Vector3.zero;
/*Vector3 horizontalVel = _rigidbody.velocity;
if(horizontalLimiterOnly)   //If we only want to stop the horizontal velocity
horizontalVel.y = 0;*/
//Calculate max allowed speed
float maxSpeed = characterInfo.maxSpeed * maxSpeedCoef;

if (localVelocity.x - maxSpeed > 0)
force.x = localVelocity.x - maxSpeed;
if (!horizontalLimiterOnly && localVelocity.y - maxSpeed > 0)
force.y = localVelocity.y - maxSpeed;
if (localVelocity.z - maxSpeed > 0)
force.z = localVelocity.z - maxSpeed;

force = directionTransform.TransformDirection(force);

_rigidbody.AddForce(-force * (speedPerOne > maxSpeedCoef ? speedPerOne - maxSpeedCoef : 0) * decelSpeed, ForceMode.VelocityChange);
}
``````

variables :

``````    public float speedPerOne { get { return speed / characterInfo.maxSpeed; } }
``````