How to set up a directional pushback system

What I’m currently working on involves a player character knocking back an enemy in the direction that the enemy is relative to the player. However I haven’t got it working exactly as I hoped.


There’s the script for the player’s hitbox

public class HitBoxManager : MonoBehaviour
{	
	void OnTriggerEnter2D (Collider2D other)
	{
		if (other.gameObject.transform.root.gameObject.CompareTag("Enemy"))
		{
			float RelativeAngle;
			Vector2 DirectionVector;
			
			other.gameObject.GetComponentInParent<EntityAttack>().AttackAnimEnd();
			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));
			other.gameObject.GetComponentInParent<GlobalEntityStatistics>().FlinchPushbackDirection = this.gameObject.GetComponentInParent<PlayerAttack>().m_CurrentKnockbackDirection * DirectionVector;
		}
	}
}

Though some of it is omitted this is what decides what direction the enemy should be pushed back when they are hit.


GlobalEntityStatistics is a script on the enemy which has the variables for the direction of pushback to apply on an enemy when it flinches.


Here’s the script on the enemy that moves the enemy when it is flinching.

public class Enemy00Movement : GlobalEntityMovement
{
	void Update ()
	{
		if (m_GlobalEntityStatistics.CurrentFlinchTimer > 0f)
		{
			m_LockMovement = true;
			m_GlobalEntityStatistics.CurrentFlinchTimer -= Time.deltaTime;
		}
	}
	
	void FixedUpdate ()
	{
		if (m_GlobalEntityStatistics.CurrentFlinchTimer > 0f)
		{
			//Calculate movement
			Vector2 movement = transform.TransformVector(transform.right * m_GlobalEntityStatistics.FlinchPushback * m_GlobalEntityStatistics.FlinchPushbackDirection * Time.deltaTime);
			//Apply movement to rigidbody
			m_rigidbody2D.MovePosition(m_rigidbody2D.position + movement);
		}
	}
}

Unfortunately this doesn’t really push them in the correct direction and isn’t really very consistent. Can anyone see where I might be going wrong with this? Yes this is most definitely complicated.

Ok I have got the pushback going in the right direction. Though I wouldn’t say it’s fully consistent. The way it was done was getting the angle between the player and the enemy get the dot product between the right of the player and the Enemy position to get an angle in 360 degrees. Then that angle is converted to a Vector 2 by calculating the cosine of the angle for the x axis and the sine of the angle for the Y. This Vector multiplied by the knock back vector of the attack is then applied to the enemy’s Rigidbody.


My problem was the attack knock back vector which was (1,0) when it should have been (0, -1)