我试图让它在我的EnemyAI脚本中,当他们被子弹击中时,他们得到击退。我想用AddForce来代替,但我不知道该怎么改,有人能帮帮我吗?我的敌人在这段代码中已经有一个名为rb的Rigidbody2D。先谢谢你
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Bullet")
{
Vector2 difference = transform.position - other.transform.position;
transform.position = new Vector2(transform.position.x + difference.x, transform.position.y + difference.y);
}
}
public float knockbackForce;
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Bullet")
{
Vector2 difference = (transform.position - other.transform.position).normalized;
Vector2 force = difference * knockbackForce;
rb.AddForce(force, ForceMode.Impulse); //if you don't want to take into consideration enemy's mass then use ForceMode.VelocityChange
}
}