Why is my knockback direction script inconsistent with it's knockback direction?
I'm trying to figure out how to get my enemy knockback working consistently. I want to push the enemy back in the direction relative to it's angle from the player. However sometimes It will push the enemy towards the player instead.
to visualise what I am after:
And here are the lines of code involved in the calculation of the pushback direction:
other.gameObject.GetComponentInParent<GlobalEntityStatistics>().FlinchPushback = this.gameObject.GetComponentInParent<PlayerAttack>().m_CurrentKnockbackSpeed;
RelativeAngle = Vector2.Angle(this.gameObject.transform.root.transform.position, other.gameObject.transform.root.transform.position);
if (Vector2.Dot(this.gameObject.transform.root.transform.right, other.gameObject.transform.position) <= 0f)
RelativeAngle = RelativeAngle + 180f;
DirectionVector = new Vector2(Mathf.Cos(RelativeAngle), Mathf.Sin(RelativeAngle));
Debug.Log(RelativeAngle + " " + DirectionVector);
other.gameObject.GetComponentInParent<GlobalEntityStatistics>().FlinchPushbackDirection = this.gameObject.GetComponentInParent<PlayerAttack>().m_CurrentKnockbackDirection * DirectionVector.normalized;
Debug.Log(other.gameObject.GetComponentInParent<GlobalEntityStatistics>().FlinchPushbackDirection);
So can anyone figure out what's wrong here?
Answer by lgarczyn · Dec 30, 2019 at 01:33 AM
WIth all due respect, that code is a mess. Just cleaning up a bit gives this:
GlobalEntityStatistics entityStat = other.GetComponentInParent<GlobalEntityStatistics>();
PlayerAttack attack = GetComponentInParent<PlayerAttack>();
entityStat.FlinchPushback = attack.m_CurrentKnockbackSpeed;
RelativeAngle = Vector2.Angle(transform.root.position, other.transform.root.position);
if (Vector2.Dot(transform.root.right, other.transform.position) <= 0f)
RelativeAngle = RelativeAngle + 180f;
DirectionVector = new Vector2(Mathf.Cos(RelativeAngle), Mathf.Sin(RelativeAngle));
Debug.Log(RelativeAngle + " " + DirectionVector);
entityStat.FlinchPushbackDirection = attack.m_CurrentKnockbackDirection * DirectionVector.normalized;
Debug.Log(entityStat.FlinchPushbackDirection);
That's still hard to read, but you can see that you are getting the Angle between two positions. This makes no sense, positions are vectors, sure, but the angle of the triangle they form with the world origin is of no interest to us.
Pushback direction would simply be
entityStat.FlinchPushbackDirection = (other.transform.position - transform.position).normalized;
That certainly cleans things up, but I still suffer from the problem of knockback direction sometimes being towards the player not away.
That's pretty weird. Is it possible the position of the object contact the script is actually on the other side of the enemy?
Well I'm using the root transform of the object the script is attached to, which is the player and the root transform of the enemy hurtbox which is the enemy, so it seems unlikely. I've been logging the value of the pushback direction each hit and yes it does vary between positive and negative vectors. I got these vectors (0.0, -0.1), (0.0, -0.7), (0.0, 0.1), (0.0, 0.0), (0.0, -0.3), (0.0, 0.4) (0.0, 0.2)
Adendum: I tried subtracting the enemy transform position from the player's transform position and essentially that makes it knock back at greater distances, but pull towards if at a closer distance. Odd.