What is the purpose for normalizing our Vector2 in this situation?

Hello all! I am currently working on the 2D RPG combat course. We are adding a knockback effect so that when we swing at enemies, they get pushed back a bit. To do this , we are using this line:

rb.AddForce(difference, ForceMode2D.Impulse);

In this situation, “difference” is a Vector2. To calculate difference, we are using this line:

Vector2 difference = (transform.position - damageSource.transform.position).normalized * knockBackThrust * rb.mass;

I wanted to ask what exactly the purpose is for normalizing our Vector2 in this instance. What would happen if we were not to normalize it? Any clarification would be appreciated. Enjoying the course so far!!

If we don’t normalize the vector, we’ll get unpredictable results…
Normalizing a Vector recalculates the Vector so that it has a magnitude of 1. Without normalizing it, the Vector could have a magnitude of .5 or 2… that would mean that even though we have a consistent Knockbackthrust set, we could have a great deal of variation in the actual amount of knockback. By Normalizing the Vector, we’re ensuring that the Knockback portion of the force is consistent.

Ah I see Brian, thank you. So basically, we are making the magnitude 1 because we are only interested in the direction between the two positions and not the magnitude. Thanks again for clarifying!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms