// Update is called once per frame void Update() { Vector3 startPoint = transform.position; Vector3 direction = transform.forward; Vector3 endPoint = startPoint + direction * distance; Ray ray = new Ray(startPoint, direction); switch (castType) { case PhysicsType.BoxSingle: if (enableInEditor) { Physics.BoxCast(startPoint, Vector3.one, direction, transform.rotation, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.BoxCast(startPoint, Vector3.one, direction, transform.rotation, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.BoxAll: if (enableInEditor) { Physics.BoxCastAll(startPoint, Vector3.one, direction, transform.rotation, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.BoxCastAll(startPoint, Vector3.one, direction, transform.rotation, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.BoxNonAlloc: if (enableInEditor) { Physics.BoxCastNonAlloc(startPoint, Vector3.one, direction, results, transform.rotation, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.BoxCastNonAlloc(startPoint, Vector3.one, direction, results, transform.rotation, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.CapsuleSingle: if (enableInEditor) { Physics.CapsuleCast(startPoint - transform.up * 0.5f, startPoint + transform.up * 0.5f, 1, direction, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.CapsuleCast(startPoint - transform.up * 0.5f, startPoint + transform.up * 0.5f, 1, direction, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.CapsuleAll: if (enableInEditor) { Physics.CapsuleCastAll(startPoint - transform.up * 0.5f, startPoint + transform.up * 0.5f, 1, direction, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.CapsuleCastAll(startPoint - transform.up * 0.5f, startPoint + transform.up * 0.5f, 1, direction, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.CapsuleNonAlloc: if (enableInEditor) { Physics.CapsuleCastNonAlloc(startPoint - transform.up * 0.5f, startPoint + transform.up * 0.5f, 1, direction, results, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.CapsuleCastNonAlloc(startPoint - transform.up * 0.5f, startPoint + transform.up * 0.5f, 1, direction, results, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.Line: if (enableInEditor) { Physics.Linecast(startPoint, endPoint, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.Linecast(startPoint, endPoint, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.RaySingle: if (useRay) { if (enableInEditor) { Physics.Raycast(ray, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.Raycast(ray, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } else { if (enableInEditor) { Physics.Raycast(startPoint, direction, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.Raycast(startPoint, direction, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } break; case PhysicsType.RayAll: if (useRay) { if (enableInEditor) { Physics.RaycastAll(ray, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.RaycastAll(ray, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } else { if (enableInEditor) { Physics.RaycastAll(startPoint, direction, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.RaycastAll(startPoint, direction, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } break; case PhysicsType.RayNonAlloc: if (useRay) { if (enableInEditor) { Physics.RaycastNonAlloc(ray, results, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.RaycastNonAlloc(ray, results, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } else { if (enableInEditor) { Physics.RaycastNonAlloc(startPoint, direction, results, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.RaycastNonAlloc(startPoint, direction, results, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } break; case PhysicsType.SphereSingle: if (useRay) { if (enableInEditor) { Physics.SphereCast(ray, 1, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.SphereCast(ray, 1, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } else { if (enableInEditor) { Physics.SphereCast(startPoint, 1, direction, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.SphereCast(startPoint, 1, direction, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } break; case PhysicsType.SphereAll: if (useRay) { if (enableInEditor) { Physics.SphereCastAll(ray, 1, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.SphereCastAll(ray, 1, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } else { if (enableInEditor) { Physics.SphereCastAll(startPoint, 1, direction, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.SphereCastAll(startPoint, 1, direction, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } break; case PhysicsType.SphereNonAlloc: if (useRay) { if (enableInEditor) { Physics.SphereCastNonAlloc(ray, 1, results, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.SphereCastNonAlloc(ray, 1, results, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } else { if (enableInEditor) { Physics.SphereCastNonAlloc(startPoint, 1, direction, results, distance, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.SphereCastNonAlloc(startPoint, 1, direction, results, distance, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } } break; case PhysicsType.CheckBox: if (enableInEditor) { Physics.CheckBox(startPoint, Vector3.one * 3, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.CheckBox(startPoint, Vector3.one * 3, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.CheckCapsule: if (enableInEditor) { Physics.CheckCapsule(startPoint, endPoint, 3, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.CheckCapsule(startPoint, endPoint, 3, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.CheckSphere: if (enableInEditor) { Physics.CheckSphere(startPoint, 3, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.CheckSphere(startPoint, 3, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.OverlapBox: if (enableInEditor) { Physics.OverlapBox(startPoint, Vector3.one * 3, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.OverlapBox(startPoint, Vector3.one * 3, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.OverlapBoxNonAlloc: if (enableInEditor) { Physics.OverlapBoxNonAlloc(startPoint, Vector3.one * 3, colliderResults, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.OverlapBoxNonAlloc(startPoint, Vector3.one * 3, colliderResults, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.OverlapCapsule: if (enableInEditor) { Physics.OverlapCapsule(startPoint, endPoint, 3, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.OverlapCapsule(startPoint, endPoint, 3, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.OverlapCapsuleNonAlloc: if (enableInEditor) { Physics.OverlapCapsuleNonAlloc(startPoint, endPoint, 3, colliderResults, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.OverlapCapsuleNonAlloc(startPoint, endPoint, 3, colliderResults, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.OverlapSphere: if (enableInEditor) { Physics.OverlapSphere(startPoint, 3, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.OverlapSphere(startPoint, 3, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; case PhysicsType.OverlapSphereNonAlloc: if (enableInEditor) { Physics.OverlapSphereNonAlloc(startPoint, 3, colliderResults, Physics.PreviewCondition.Editor, drawDuration, hitColor, noHitColor); } if (enableInRuntime) { Physics.OverlapSphereNonAlloc(startPoint, 3, colliderResults, Physics.PreviewCondition.Game, drawDuration, hitColor, noHitColor); } break; } }
/// <summary> /// Perform a ranged attack /// </summary> /// <param name="_foe">Unit</param> public void RangedAttack(Unit _foe) { RaycastHit[] allHits = Physics.RaycastAll(transform.position, transform.TransformDirection(Vector3.forward), m_AttackRange * 0.5f); RaycastHit directHit; if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out directHit, m_AttackRange * 0.5f, RotaryHeart.Lib.PhysicsExtension.PreviewCondition.Game, 80f, Color.cyan, Color.red)) { Debug.Log(directHit.collider.tag); } if (m_Animation) { m_Animation.PlayRangedAttackAnimation(m_UnitAttributes.attacks); } for (int j = 0; j < m_UnitAttributes.troopSize && _foe.m_UnitAttributes.troopSize > 0; j++) { bool notFailed = false; foreach (RaycastHit hit in allHits) { if (hit.transform.parent.name == _foe.transform.name) { notFailed = true; } } // Allocate if unit hits the foe, uses ballistic skill if (notFailed && directHit.collider != null && directHit.collider.tag == "FullCover") { notFailed = false; } int roll = Random.Range(1, 6 + 1); //Debug.Log("Hit Roll: " + roll); if (notFailed && 6 - m_UnitAttributes.ballisticSkill > roll) { Debug.Log(this.name + "'s attack has failed! (Hit)"); notFailed = false; } // Allocate if unit hurts the foe if (notFailed) { notFailed = HurtOppenent(this, _foe); } // Allocate if foes save value saves it // TODO: Will be extended to support cover values of terrain roll = Random.Range(1, 6 + 1); if (directHit.collider != null && directHit.collider.tag == "Cover") { roll += 2; } //Debug.Log("Save Roll: " + roll); if (notFailed && _foe.m_UnitAttributes.save <= roll) { Debug.Log(this.name + "'s attack has failed! (Save)"); notFailed = false; } if (notFailed) { _foe.ReceiveDamage(1); Debug.Log(this.name + "'s attack was successfull!"); } } }