Question by
jackychiu02 · Aug 20, 2021 at 02:16 PM ·
knockback
questions about knockback(2D)
What I wanna do is when objectA collide with objectB, ObjectB will have a knockback. Before that, ObjectB is moving towards a point with the help of Vector2.MoveTowards. However, when object A collide with ObjectB, ObjectB will move away due to the collision. How can i restrict Object B to do the knockback only because of a rb.AddForce instead of the collision?
private void MovePosition(){
moveSpot.position = new Vector2(Random.Range(MinPos.position.x,MaxPos.position.x),Random.Range(MinPos.position.y,MaxPos.position.y));
waitTime = startWaitTime;
}
private void OnTriggerEnter2D(Collider2D other) { if(other.CompareTag("Hitbox")){ StartCoroutine("KnockbackCo"); } } private IEnumerator KnockbackCo(){ KnockbackDir = rb.transform.position - hitBox.transform.position; Debug.Log(KnockbackDir); rb.AddForce(KnockbackDir.normalized*1000,ForceMode2D.Impulse); yield return new WaitForSeconds(0.2f); }
Comment
Your answer
