// 磁力の範囲に入った時に呼ばれる public override void OnCircleTrigger2D(PoleArea mine, PoleArea other) { if (m_pole == Pole.None) { return; } if (other.m_poleObject.m_pole == Pole.None) { return; } if (m_type == Type.NoMove_Change || m_type == Type.NoMove_NoChange) { return; } Vector2 dir = other.transform.position - mine.transform.position; float distance = dir.magnitude; float border = mine.m_radius + other.m_radius; if (border <= 0.0f) { return; } Vector2 forceDir; forceDir = (m_pole == other.m_poleObject.m_pole) ? -dir : dir; Vector2 add = forceDir.normalized * (1.0f - distance / border); AddPoleForce(add); }
// 磁力の範囲に入った時に呼ばれる public override void OnCircleTrigger2D(PoleArea mine, PoleArea other) { if (m_pole == Pole.None) { return; } if (other.m_poleObject.m_pole == Pole.None) { return; } Vector2 dir = other.transform.position - mine.transform.position; float distance = dir.magnitude; float border = mine.m_radius + other.m_radius; if (border <= 0.0f) { return; } // レイキャストを当たったオブジェクトの方向に飛ばす // 何でもいいので最初に衝突したコリジョンの法線の方向に力を加える RaycastHit2D[] castHit; RaycastHit2D? result = null; castHit = Physics2D.RaycastAll(mine.transform.position, dir.normalized, distance); foreach (var hit in castHit) { if (transform == hit.collider.transform.parent) { continue; } if (result.HasValue) { if (result.Value.distance > hit.distance) { result = hit; } } else { result = hit; } } if (!result.HasValue) { return; } Vector2 forceDir; forceDir = (m_pole == other.m_poleObject.m_pole) ? result.Value.normal : -result.Value.normal; Vector2 add = forceDir.normalized * (1.0f - distance / border); AddPoleForce(add); }
// インスペクターが変更せれると呼ばれる protected void OnDrawGizmos() { PoleSprite[] sprites = GetComponentsInChildren <PoleSprite>(); foreach (var s in sprites) { s.PoleChange(this); } PoleArea area = GetComponentInChildren <PoleArea>(); if (area) { area.Gizomos(this); } }
private void OnTriggerStay2D(Collider2D collision) { if (!collision.isTrigger) { return; } PoleArea otherArea = collision.GetComponent <PoleArea>(); if (!otherArea) { return; } m_poleObject.OnCircleTrigger2D(this, otherArea); }
public override void OnStart() { base.OnStart(); m_poleSprites = GetComponentsInChildren <PoleSprite>(); foreach (var s in m_poleSprites) { s.PoleChange(this); } m_poleArea = GetComponentInChildren <PoleArea>(); if (m_poleArea) { m_poleArea.SetPoleObject(this); } }
public virtual void OnCircleTrigger2D(PoleArea mine, PoleArea other) { }