You are on page 1of 2

//handles knockback for a super smash bros style game

private void FixedUpdate()


{
if (knockbackForce > 0 && hitstunCounter <= 0)
{
//create an X and Y variable to plug into a vector to add force to the player
//knockbackAmount is the base value for how far the player will get knocked back
//knockback force is a value between 0 and 1 that is controlled by an animation curve
//addForceMultiplier is a static number that allows the variable to be used with
rigidbody.AddForce()
//weight controls how easy it is to knock a player back
//PlayerHealth.health causes greater knockback if the player has been damaged more
float knockbackVectorX = knockbackAmount.x * knockbackForce * addForceMultiplier *
weight * (PlayerHealth.health * 0.05f);
float knockbackVectorY = knockbackAmount.y * knockbackForce * addForceMultiplier *
weight * (PlayerHealth.health * 0.05f);

//if the player moves while being knocked back they can influence the distance they
travel
if (knockbackVectorX > 0)
{
if (horizontalMovement > 0)
HDirectionalInfluence = 1.6f;
else if (horizontalMovement < 0)
HDirectionalInfluence = 0.6f;
else
HDirectionalInfluence = 1;
}
else if (knockbackVectorX < 0)
{
if (horizontalMovement < 0)
HDirectionalInfluence = 1.6f;
else if (horizontalMovement > 0)
HDirectionalInfluence = 0.6f;
else
HDirectionalInfluence = 1;
}

if (verticalMovement < 0)
VDirectionalInfluence = 0.6f;
else
VDirectionalInfluence = 1;
//multiplies directional influence by knockback vector
knockbackVectorX *= HDirectionalInfluence;
knockbackVectorY *= VDirectionalInfluence;

//if knockback amount is too low after calculations, set it to a noticeable value
if (knockbackVectorX < knockbackAmount.x * addForceMultiplier && knockbackVectorY <
knockbackAmount.y * addForceMultiplier)
{
knockbackVectorX = knockbackAmount.x * knockbackForce * addForceMultiplier;
knockbackVectorY = knockbackAmount.y * knockbackForce * addForceMultiplier;
}

PlayerRigidbody.AddForce(new Vector2(knockbackVectorX, knockbackVectorY)); //add


force to the rigidbody to cause knockback

knockbackForce = knockbackCurve.Evaluate(knockbackCurveTime); //scale the


amount of knockback based on a variable controlled by an animation curve

knockbackCurveTime += Time.fixedDeltaTime; //count timer up

//after 0.2 seconds give the player control again


if (knockbackCurveTime >= 0.2f)
{
movementLag = 1;
utilityLag = false;
atk.attackLag = false;
}
}
}

You might also like