void Update() { if (isDead) { return; } oldPosition = transform.position; //Check if the puppy is dying if (animator.GetBool("IsDead")) { StartCoroutine(Die()); return; } Steering(); if (currentSpeed.magnitude > speed) { currentSpeed = currentSpeed.normalized * speed; } //Check if the movement direction has changed FlipSprite(); // Calculate new position Vector2 newPos = new Vector2(transform.position.x, transform.position.y) + currentSpeed * Time.deltaTime; // Update position transform.position = new Vector3(newPos.x, newPos.y, 0.0f); GridAI.GetInstance().UpdatePosition(this.gameObject, oldPosition, newPos); }
private void Awake() { // ensrue that the first time the position is written GridAI.GetInstance().InitializePosition(this.gameObject, transform.position); }
void Steering() { currentPos = transform.position; playerPos = player.transform.position; distanceVector = playerPos - currentPos; distanceVector.Normalize(); if (Vector2.Distance(currentPos, playerPos) < detectionArea && aiStats.IsAvoidingPlayer) // Follow/flee player { desiredSpeed = distanceVector * speed; Vector3 aux = Quaternion.Euler(0, 0, 90) * desiredSpeed.normalized; desiredSpeed = aux * speed * fleeDir; } else if (!isWandering) { StartCoroutine(Wander(wanderTime)); } separationVector = new Vector2(0.0f, 0.0f); /*foreach (GameObject p in aiManager.AIList) * { * distance = currentPos - new Vector2(p.transform.position.x, p.transform.position.y); * if (distance.magnitude <= area) * { * separationVector += distance; * } * }*/ // Get the closest positions of Grid to separate //foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos)) foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos, area)) { distance = currentPos - new Vector2(go.transform.position.x, go.transform.position.y); separationVector += distance; } Vector3 auxPerp = Quaternion.Euler(0, 0, 90) * currentSpeed.normalized; Vector2 perpendicular = new Vector2(auxPerp.x, auxPerp.y); perpendicular.Normalize(); // Check sides possible collision rightSide = currentPos + perpendicular * 0.7f; Vector2 leftSide = currentPos - perpendicular * 0.7f; Debug.DrawLine(rightSide, rightSide + currentSpeed.normalized * 3.0f, Color.yellow); Debug.DrawLine(leftSide, leftSide + currentSpeed.normalized * 3.0f, Color.yellow); Vector2 wallAvoidance = new Vector2(0.0f, 0.0f); RaycastHit2D rightHit = Physics2D.Raycast(rightSide, currentSpeed.normalized, wallAvoidDistance); RaycastHit2D leftHit = Physics2D.Raycast(leftSide, currentSpeed.normalized, wallAvoidDistance); if (rightHit.collider != null && rightHit.transform.gameObject.tag != "AI" && rightHit.transform.gameObject.tag != "Player") { wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, rightHit.normal) * rightHit.normal; } else if (leftHit.collider != null && leftHit.transform.gameObject.tag != "AI" && leftHit.transform.gameObject.tag != "Player") { wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, leftHit.normal) * leftHit.normal; } currentSpeed = currentSpeed + separationVector + wallAvoidance / 2.0f; steering = desiredSpeed - currentSpeed; currentSpeed += steering * Time.deltaTime; }
private void OnDestroy() { GridAI.GetInstance().RemoveFromGrid(this.gameObject); }
void Steering() { currentPos = transform.position; playerPos = player.transform.position; Color color = Color.red; distanceVector = playerPos - currentPos; distanceVector.Normalize(); if (Vector2.Distance(currentPos, playerPos) > detectionArea) // Follow/flee player { if (!wandering) { StartCoroutine(Wander(wanderTime)); } else { color = Color.green; } } else { desiredSpeed = distanceVector * speed; if (player.GetComponent <PlayerStats>().Smell) { color = Color.yellow; Vector3 aux = Quaternion.Euler(0, 0, 90) * desiredSpeed.normalized; desiredSpeed = aux * speed * fleeDir; } } separationVector = new Vector2(0.0f, 0.0f); // iterate all elements in array /*foreach (GameObject p in aiManager.AIList) * { * distance = currentPos - new Vector2(p.transform.position.x, p.transform.position.y); * if(distance.magnitude <= area) * { * separationVector += distance; * } * }*/ /* * int i = 0; * Collider2D[] overlap = Physics2D.OverlapCircleAll(transform.position, area, LayerMask.NameToLayer("AI")); * foreach(Collider2D c in overlap) * { * i += 1; * distance = currentPos - new Vector2(c.transform.position.x, c.transform.position.y); * separationVector += distance; * * } */ // Get the closest positions to steer //foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos)) foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos, area)) { distance = currentPos - new Vector2(go.transform.position.x, go.transform.position.y); separationVector += distance; } Vector3 auxPerp = Quaternion.Euler(0, 0, 90) * currentSpeed.normalized; Vector2 perpendicular = new Vector2(auxPerp.x, auxPerp.y); perpendicular.Normalize(); // Check sides possible collision rightSide = currentPos + perpendicular * 0.7f; leftSide = currentPos - perpendicular * 0.7f; Debug.DrawLine(rightSide, rightSide + currentSpeed.normalized * 6.0f, color); Debug.DrawLine(leftSide, leftSide + currentSpeed.normalized * 6.0f, color); wallAvoidance = new Vector2(0.0f, 0.0f); RaycastHit2D rightHit = Physics2D.Raycast(rightSide, currentSpeed.normalized, wallAvoidDistance); RaycastHit2D leftHit = Physics2D.Raycast(leftSide, currentSpeed.normalized, wallAvoidDistance); if (rightHit.collider != null && rightHit.transform.gameObject.tag != "AI" && rightHit.transform.gameObject.tag != "Player") { wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, rightHit.normal) * rightHit.normal; //Debug.DrawLine(rightHit.point, rightHit.point + wallAvoidance.normalized * 1.5f, Color.green); } else if (leftHit.collider != null && leftHit.transform.gameObject.tag != "AI" && leftHit.transform.gameObject.tag != "Player") { wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, leftHit.normal) * leftHit.normal; //Debug.DrawLine(leftHit.point, leftHit.point + wallAvoidance.normalized * 1.5f, Color.green); } currentSpeed = currentSpeed + separationVector + wallAvoidance; steering = desiredSpeed - currentSpeed; currentSpeed += steering * Time.deltaTime; }