public override void CollisionDetect() { int num = collider.OverlapCollider(filter2D, res); for (int i = 0; i < num; i++) { HitableObj.Hit_event_c(res[i].gameObject, ammoData.damage); Debug.Log("hit " + res[i].gameObject.name); turns--; } if (turns > 0 && num > 0) { //Vector2 _normal = vec.x > res[0].transform.position.x ? Vector2.right : Vector2.left; Vector2 _normal = (res[0].transform.position - transform.position).magnitude > 0.3f ? vec.rotate(-45 * Mathf.Deg2Rad) : -vec; vec = Vector2.Reflect(vec, _normal); //Vector3 _in = vec.normalized; //Vector3 _normal = GetSimpleNormal(res[0].gameObject); //vec = vec.rotate(90 * Vector3.Cross(vec, _normal).z); transform.position = (Vector2)transform.position + vec.normalized; } else if (turns < 0) { //Destory self: DoDestory(); } }
void OnCollisionEnter2D(Collision2D collision) { Vector2 inDirection = dir; Vector2 inNormal = collision.contacts[0].normal.normalized; Vector2 newVelocity = Vector2.Reflect(inDirection, inNormal); Debug.DrawLine(collision.transform.position, (Vector2)collision.transform.position + inNormal * 5, Color.green, 1); Debug.DrawLine(collision.transform.position, (Vector2)collision.transform.position + newVelocity * 5, Color.cyan, 1); rigid.velocity = newVelocity * reflectSpeed; //rigid.AddForce(newVelocity * reflectSpeed); Debug.DrawLine(transform.position, (Vector2)transform.position + rigid.velocity, Color.gray, 1); HitableObj.Hit_event_c(collision.gameObject, damage); }
public virtual void CollisionDetect() { //detect collision int num = collider.OverlapCollider(filter2D, res); for (int i = 0; i < num; i++) { HitableObj.Hit_event_c(res[i].gameObject, ammoData.damage); Debug.Log(gameObject.name + " GC obj hit " + res[i].gameObject.name); } if (num > 0) { //Destory self: DoDestory(); } }