public static float PredictCollisionBySimulation(MovementAgent aMovement, MovementAgent bMovement, float minDistance, float timeStep) { if (aMovement == null || bMovement == null) { return(-1f); } float time = 0f; Vector3 posA; Vector3 posB; var upperBound = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height, 0)) + Vector3.up; var downBound = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, 0)) + Vector3.down; do { posA = aMovement.GetPosition(aMovement.CurrentTime + time); posB = bMovement.GetPosition(bMovement.CurrentTime + time); if (Mathf.Abs(Vector3.Distance(posA, posB)) <= minDistance) { return(time); } time += timeStep; }while(posA.y > downBound.y && posA.y < upperBound.y && posB.y > downBound.y && posB.y < upperBound.y); return(-1f); }
public void SetupAI(bool aiActivation) { IsAIActive = aiActivation; if (!IsAIActive) { MovementAgent.StopMovement(); ChaseTarget = null; } }
/// <summary> /// Initialize object from pool or create new /// </summary> /// <param name="movement">Movement type</param> /// <param name="position">Start position</param> /// <returns></returns> GameObject InitObject(MovementAgent movement, Vector3 position) { var obj = pool.GetObject(movement.MovementType, PrepareObj, CreateObject); obj.transform.position = position; obj.GetComponent <MovementComponent>().MovementAgent = movement; var hit = obj.GetComponent <SphereHit>(); hit.OnHit += HandleOnHit; return(obj); }
public static float PredictLineCollision(MovementAgent aMovement, MovementAgent bMovement, float minDistance) { if (aMovement == null || bMovement == null) { return(-1); } return(PredictLineCollision(aMovement.CurrentPosition, aMovement.Rotation, aMovement.Speed, bMovement.CurrentPosition, bMovement.Rotation, bMovement.Speed, minDistance)); }
void Spawn() { float minX = (extentsBound.x - originBound.x); float maxX = Screen.width - minX; float y = spawnFromTop ? Screen.height : 0; Quaternion rotation = Quaternion.identity; Vector3 position = Vector3.zero; //Choose random movement type var movements = Enum.GetValues(typeof(MovementType)); MovementType currentMovementType = (MovementType)movements.GetValue(UnityEngine.Random.Range(0, movements.Length)); MovementAgent movement = null; int countTries = numberOfTries; bool hasCollision = false; do { countTries--; var x = UnityEngine.Random.Range(minX, maxX); position = mainCamera.ScreenToWorldPoint(new Vector3(x, y, 5f)); var screenPoint = mainCamera.WorldToScreenPoint(position); rotation = GeometryUtils.FindRotation(screenPoint, minX, maxX, spawnFromTop); movement = GetMovementByType(currentMovementType, speed, position, rotation); foreach (var otherObj in objects) { float time; var otherObjMovement = otherObj.GetComponent <MovementComponent>().MovementAgent; if (otherObjMovement.MovementType == MovementType.Line && currentMovementType == MovementType.Line) { time = GeometryUtils.PredictLineCollision(movement, otherObjMovement, collisionDistance); } else { time = GeometryUtils.PredictCollisionBySimulation(movement, otherObjMovement, collisionDistance, Time.fixedDeltaTime); } if (time >= 0f) { hasCollision = true; break; } } }while(hasCollision && countTries > 0); if (!hasCollision) { var obj = InitObject(movement, position); objects.Add(obj); StatisticSystem.InGame++; StatisticSystem.Started++; } else { StatisticSystem.Canceled++; } spawnFromTop = !spawnFromTop;//switch side }