void OnTriggerEnter2D(Collider2D collider) { if (collider is BoxCollider2D && collider.GetComponent <CreepScript>() != null && collider.GetComponent <CreepScript>().WayPointTarget == transform) { UtilityFunctions.DebugMessage("Finding waypoint through collider - " + collider.name); Transform wayPointTarget = collider.GetComponent <CreepScript>().WayPointTarget; Transform target = collider.GetComponent <CreepScript>().Target; wayPointTarget = PathingScript.FindClosestWayPointToSelfAndTarget(transform, wayPointTarget, target); collider.GetComponent <CreepScript>().WayPointTarget = wayPointTarget ?? target; } }
void Start() { powerUsed = 0.0f; rb = gameObject.GetComponent <Rigidbody>() as Rigidbody; finished = true; upt = GameObject.Find("Canvas").GetComponent <UIPowerTracker>() as UIPowerTracker; weather = GameObject.Find("Weather").GetComponent <Weather>() as Weather; pathingScript = gameObject.GetComponent <PathingScript>() as PathingScript; currentStart = weather.zonesByID[startingZoneID]; currentEnd = weather.zonesByID[baseDestinationZoneID]; path = new List <Zone>(); pathProgress = 0; }
// Update is called once per frame void Update() { if (WayPointTarget == null) { UtilityFunctions.DebugMessage("Finding waypoint through update"); WayPointTarget = PathingScript.FindClosestWayPointToSelfAndTarget(transform, WayPointTarget, Target); if (WayPointTarget == null) { WayPointTarget = Target; } } if (!Afflictions.Any(x => x.AfflictionType == AfflictionTypes.Stun)) { if (IsFighting && FightingTarget != null) { if ((UtilityFunctions.UseUnitZPosition(transform, transform.position) - UtilityFunctions.UseUnitZPosition(transform, FightingTarget.position)).sqrMagnitude > DistanceBetweenMeleeFighters) { UtilityFunctions.DebugMessage("Moving towards melee target."); IsMovingTowardFighter = true; float unitSpeedAdjustment = GetUnitSpeedAdjustment(); float newUnitSpeed = unitSpeedAdjustment * UnitSpeed; transform.position += (UseUnitZPosition(FightingTarget.position) - transform.position).normalized * Time.deltaTime * newUnitSpeed; } else { IsMovingTowardFighter = false; Attack(); } } else if (IsFighting) { IsMovingTowardFighter = false; IsFighting = false; } else { float unitSpeedAdjustment = GetUnitSpeedAdjustment(); float newUnitSpeed = unitSpeedAdjustment * UnitSpeed; transform.position += (UseUnitZPosition(WayPointTarget.transform.position) - transform.position).normalized * Time.deltaTime * newUnitSpeed; } } Afflictions.RemoveAll(x => x.EndTime <= Time.time); }
void Start() { // Get all paths var paths = PathingScript.BuildAIPaths(); // Find all intersections of paths, remove duplicates List <ActualVector> collisionPaths = new List <ActualVector>(); foreach (var path in paths) { for (var index = 0; index < path.Count; index++) { if (index != 0) { Vector3 nodePath = UtilityFunctions.UseUnitZPosition(transform, path[index]) - UtilityFunctions.UseUnitZPosition(transform, path[index - 1]); RaycastHit2D[] hits = Physics2D.RaycastAll(UtilityFunctions.UseUnitZPosition(transform, path[index - 1]), nodePath, nodePath.magnitude); if (hits.Any(x => x.collider == transform.GetComponent <Collider2D>()) || transform.GetComponent <Collider2D>().OverlapPoint(path[index - 1])) { var collisionPoint1 = hits.Any(x => x.collider == transform.GetComponent <Collider2D>()) ? hits.First(y => y.collider == transform.GetComponent <Collider2D>()).point : (Vector2?)null; if (!collisionPaths.Any(x => x.IsEqual(path[index - 1], nodePath, nodePath.magnitude, collisionPoint1))) { collisionPaths.Add(new ActualVector { Origin = path[index - 1], Direction = nodePath, Magnitude = nodePath.magnitude, CollisionPoint1 = collisionPoint1, StartsInsideCollider = transform.GetComponent <Collider2D>().OverlapPoint(path[index - 1]), EndsInsideCollider = transform.GetComponent <Collider2D>().OverlapPoint(path[index]) }); } } } } } // refine vectors to only inside the collider for (var pathIndex = 0; pathIndex < collisionPaths.Count; pathIndex++) { // if outside collider, set origin to collider, shorten magnitude if (!collisionPaths[pathIndex].StartsInsideCollider) { var newVector = collisionPaths[pathIndex].Direction - (collisionPaths[pathIndex].CollisionPoint1.GetValueOrDefault() - collisionPaths[pathIndex].Origin); collisionPaths[pathIndex].Origin = collisionPaths[pathIndex].CollisionPoint1.GetValueOrDefault(); collisionPaths[pathIndex].Direction = newVector; collisionPaths[pathIndex].Magnitude = newVector.magnitude; } // if vector leaving collider, cut off if (!collisionPaths[pathIndex].EndsInsideCollider) { var endPoint = collisionPaths[pathIndex].Direction + collisionPaths[pathIndex].Origin; var oppositeDirectection = collisionPaths[pathIndex].Direction * -1; RaycastHit2D[] hits = Physics2D.RaycastAll(UtilityFunctions.UseUnitZPosition(transform, endPoint), oppositeDirectection, oppositeDirectection.magnitude); var collision = hits.First(x => x.collider == transform.GetComponent <Collider2D>()).point; collisionPaths[pathIndex].Direction = collision - collisionPaths[pathIndex].Origin; collisionPaths[pathIndex].Magnitude = (collision - collisionPaths[pathIndex].Origin).magnitude; } } CollisionPaths = collisionPaths; }