public override void Update(Unit unit) { TryRecall(unit); if (!hasPath) { if (closestBullet != null) { unit.target = closestBullet.transform; targetVector = new Vector2(closestBullet.StartPosition.x, closestBullet.StartPosition.y); PathRequestManager.RequestPath(unit.Rb.position, targetVector, unit.OnPathFound); hasPath = true; } else { closestBullet = FindClosestBullet(unit.transform.position); } } if (unit.CurrentWeapon.BulletCount >= unit.CurrentWeapon.MagazineSize) { unit.ChangeState(unit.stateHuntingPlayer); } //TODO: this section solely exists to make sure the enemy doesn't get stuck, which happens more than I'd like to admit currentPosition = unit.transform.position; if (currentPosition == previousPosition) { fixTimer += Time.deltaTime; if (fixTimer > 3f) { unit.recallRadius = 100f + Time.deltaTime; } } previousPosition = currentPosition; }
public override IEnumerator ReverseMovement(LoopableObject loopableObject) { while (true) { if (loopableObject.InternalTime <= loopableObject.HalfLoopDuration) //this must be in an if statement since the internal time goes one frame past the half loop duration which messes up the easing (object starts to move backwards before it has reversed) { //collider.radius = startRadius / EaseInQuadraticInterpolate(1f, loopableObject.EndSize, loopableObject.InternalTime / loopableObject.HalfLoopDuration); collider.radius = startRadius * Mathf.Lerp(loopableObject.EndSize, 1f, loopableObject.InternalTime / loopableObject.HalfLoopDuration); } yield return(new WaitForFixedUpdate()); //increment time at a fixed rate to ensure coroutine timings are perfect } }
public override IEnumerator ReverseMovement(LoopableObject loopableObject) { while (true) { if (loopableObject.InternalTime <= loopableObject.HalfLoopDuration) //this must be in an if statement since the internal time goes one frame past the half loop duration which messes up the easing (object starts to move backwards before it has reversed) { endPosition = loopableObject.StartPosition + upVector * loopableObject.Speed * (loopableObject.HalfLoopDuration / Time.maximumDeltaTime); loopableObject.transform.position = EaseInQuadraticVector2(endPosition, loopableObject.StartPosition, loopableObject.InternalTime / loopableObject.HalfLoopDuration); } yield return(new WaitForFixedUpdate()); //increment time at a fixed rate to ensure coroutine timings are perfect } }
public override IEnumerator ForwardMovement(LoopableObject loopableObject) { while (true) { if (loopableObject.InternalTime <= loopableObject.HalfLoopDuration) //this must be in an if statement since the internal time goes one frame past the half loop duration which messes up the easing (object starts to move backwards before it has reversed) { //collider.size = new Vector2(startSize.x, startSize.y * EaseOutQuadraticInterpolate(1f, loopableObject.EndSize, loopableObject.InternalTime / loopableObject.HalfLoopDuration)); collider.size = new Vector2(startSize.x, startSize.y * Mathf.Lerp(1f, loopableObject.EndSize, loopableObject.InternalTime / loopableObject.HalfLoopDuration)); offsetY = -endHeight / 2 + Mathf.Lerp(startSize.y / 2, endHeight / 2, loopableObject.InternalTime / loopableObject.HalfLoopDuration); collider.offset = new Vector2(0f, offsetY); } yield return(new WaitForFixedUpdate()); //increment time at a fixed rate to ensure coroutine timings are perfect } }
public override IEnumerator ForwardMovement(LoopableObject loopableObject) { while (true) { if (loopableObject.InternalTime <= loopableObject.HalfLoopDuration) //this must be in an if statement since the internal time goes one frame past the half loop duration which messes up the easing (object starts to move backwards before it has reversed) { Vector2 currentPosition = new Vector2(loopableObject.transform.position.x, loopableObject.transform.position.y); //float easingFactor = EaseOutQuadratic(loopableObject.InternalTime / loopableObject.HalfLoopDuration); float xDelta = circleCenter.x + Mathf.Cos(loopableObject.InternalTime * loopableObject.Speed * 10f) * circleRadius; float yDelta = circleCenter.y - Mathf.Sin(loopableObject.InternalTime * loopableObject.Speed * 10f) * circleRadius; loopableObject.transform.position = new Vector3(xDelta, yDelta, 0); circleRadius += circleExpansionSpeed; } yield return(new WaitForFixedUpdate()); //increment time at a fixed rate to ensure coroutine timings are perfect } }
public LoopableObject FindClosestBullet(Vector2 currentPosition) { float shortestDistance = Mathf.Infinity; LoopableObject closest = null; foreach (LoopableObject bullet in firedBullets) { if (bullet != null) { Vector2 distanceFromFirer = bullet.StartPosition - currentPosition; float bulletDistance = distanceFromFirer.sqrMagnitude; if (bulletDistance < shortestDistance) { closest = bullet; shortestDistance = bulletDistance; } } } return(closest); }
public void TryRecall(Unit unit) { Collider2D[] nearbyObjects = Physics2D.OverlapCircleAll(unit.transform.position, unit.recallRadius); for (int i = 0; i < nearbyObjects.Length; i++) { if (nearbyObjects[i].GetComponent <LoopableObject>() != null) { if (nearbyObjects[i].GetComponent <LoopableObject>().IsReversing) { if (nearbyObjects[i].GetComponent <LoopableObject>().originator == unit.CurrentWeapon.gameObject) { firedBullets.Remove(closestBullet); closestBullet = null; hasPath = false; unit.CurrentWeapon.Recall(nearbyObjects[i].gameObject); return; //prevents recalling multiple bullets at once } } } } }
public abstract IEnumerator ForwardMovement(LoopableObject loopableObject);
public abstract void Initialize(LoopableObject loopableObject);
public abstract IEnumerator ReverseMovement(LoopableObject loopableObject);
private float startRadius; //the initial radius of the object's collider public override void Initialize(LoopableObject loopableObject) { collider = loopableObject.GetComponent <CircleCollider2D>(); startRadius = collider.radius; }
private float offsetY; //the y-offset of the box collider public override void Initialize(LoopableObject loopableObject) { collider = loopableObject.GetComponent <BoxCollider2D>(); startSize = collider.size; endHeight = collider.size.y * loopableObject.EndSize; }
public override void Initialize(LoopableObject loopableObject) { upVector = new Vector2(loopableObject.transform.right.x, loopableObject.transform.right.y); }
public override void Initialize(LoopableObject loopableObject) { upVector = new Vector2(loopableObject.transform.right.x, loopableObject.transform.right.y); circleCenter = loopableObject.StartPosition - upVector; circleExpansionSpeed = loopableObject.Speed / 10f; //how quickly the circle that the object revolves around expands }