0
votes
***

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Knockback : MonoBehaviour { public float thrust; public float KnockTime;

// Start is called before the first frame update
void Start()
{
    
}
// Update is called once per frame
void Update()
{
    
}
private void OnTriggerEnter2D(Collider2D other)
{
    if(other.gameObject.CompareTag("enemy"))
    {
        Rigidbody2D enemy = other.GetComponent<Rigidbody2D>();
        if(enemy != null)
        {
            StartCoroutine(KnockCo(enemy));
        }
    }
}
private IEnumerator KnockCo(Rigidbody2D enemy)
{
    if(enemy != null)
    {
        Vector2 forceDirection = enemy.transform.position - transform.position;
        Vector2 force = forceDirection.Normalize() * thrust;
        
        enemy.velocity = force;
        yield return new WaitForSeconds(KnockTime);
       
       enemy.velocity = new Vector2();
       {
       }
    }
} }

1

1 Answers

0
votes

forceDirection.Normalize() alters the forceDirection vector and returns void, rather than returning the normalized vector. So you'll need to split the Normalize() call and the multiplication into separate statements:

Vector2 forceDirection = enemy.transform.position - transform.position;
forceDirection.Normalize();
Vector2 force = forceDirection * thrust;