/// <summary> /// Swim between random positions /// </summary> private void Swim() { // If it's time for the next action, pick a new speed and destination // Else, swim toward the destination if (Time.fixedTime >= nextActionTime) { // Randomize the speed randomizedSpeed = fishSpeed * UnityEngine.Random.Range(.5f, 1.5f); // Pick a random target targetPosition = PenguinArea.ChooseRandomPosition(transform.parent.position, 100f, 260f, 2f, 13f); // Rotate toward the target transform.rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.up); // Calculate the time to get there float timeToGetThere = Vector3.Distance(transform.position, targetPosition) / randomizedSpeed; nextActionTime = Time.fixedTime + timeToGetThere; } else { // Make sure that the fish does not swim past the target Vector3 moveVector = randomizedSpeed * transform.forward * Time.fixedDeltaTime; if (moveVector.magnitude <= Vector3.Distance(transform.position, targetPosition)) { transform.position += moveVector; } else { transform.position = targetPosition; nextActionTime = Time.fixedTime; } } }
private void Swim() { if (Time.fixedTime >= nextActionTime) { randomizedSpeed = fishSpeed * UnityEngine.Random.Range(0.5f, 1.5f); targetPosition = PenguinArea.ChooseRandomPosition(transform.parent.position, 100f, 260f, 2f, 13f); transform.rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.up); float timeToGetThere = Vector3.Distance(transform.position, targetPosition) / randomizedSpeed; nextActionTime = Time.fixedTime + timeToGetThere; } else { Vector3 moveVector = randomizedSpeed * transform.forward * Time.fixedDeltaTime; if (moveVector.magnitude <= Vector3.Distance(transform.position, targetPosition)) { transform.position += moveVector; } else { transform.position = targetPosition; nextActionTime = Time.fixedTime; } } }
private void Swim() { if (Time.fixedTime >= nextActionTime) { // randomised the speed randomisedSpeed = fishSpeed * UnityEngine.Random.Range(.5f, 1.5f); // pic the target position targetPosition = PenguinArea.ChooseRandomPosition(transform.parent.position, 260f, 100f, 13f, 2f); //Rotate the target transform.rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.up); // calculate time to get there float timeToGetThere = Vector3.Distance(transform.position, targetPosition) / randomisedSpeed; nextActionTime = Time.fixedTime + timeToGetThere; } else { // Make sure that the fish does not past the target Vector3 moveVector = randomisedSpeed * Time.fixedDeltaTime * transform.forward; if (moveVector.magnitude <= Vector3.Distance(transform.position, targetPosition)) { transform.position += moveVector; } else { transform.position = targetPosition; nextActionTime = Time.fixedTime; } } }
private void Swim() { if (Time.fixedTime >= timeNextAction) { // Randomize the speed. speedRandomized = speedFish * UnityEngine.Random.Range(0.5f, 1.5f); // Pick a random target. positionTarget = PenguinArea.ChooseRandomPosition(transform.parent.position, 100.0f, 260.0f, 2.0f, 13.0f); // Rotate toward the target. transform.rotation = Quaternion.LookRotation(positionTarget - transform.position, Vector3.up); // Calculate the time to get there. float timeToGetThere = Vector3.Distance(transform.position, positionTarget) / speedRandomized; timeNextAction = Time.fixedTime + timeToGetThere; } else { // Make sure that the fish does not swim past the target. Vector3 moveVector = speedRandomized * transform.forward * Time.fixedDeltaTime; if (moveVector.magnitude <= Vector3.Distance(transform.position, positionTarget)) { transform.position += moveVector; } else { transform.position = positionTarget; timeNextAction = Time.fixedTime; } } }
/// <summary> /// Nada entre posições aleatorias /// </summary> private void Swim() { //Se for a hora para proxima ação, pega uma nova velocidade e destino //Senão nada para o destino if (Time.fixedTime >= nextActionTime) { //Velocidade aleatoria randomizedSpeed = fishSpeed * UnityEngine.Random.Range(.5f, 1.5f); //Pega um algo aleatorio targetPosition = PenguinArea.ChooseRandomPosition(transform.parent.position, 100f, 260f, 2f, 13f); //Rotaciona em direção ao alvo transform.rotation = Quaternion.LookRotation(targetPosition - transform.position, Vector3.up); //Calcula o tempo para chegar lá float timeToGetThere = Vector3.Distance(transform.position, targetPosition) / randomizedSpeed; nextActionTime = Time.fixedTime + timeToGetThere; } else { //Garante que o peixe não nade alem do alvo Vector3 moveVector = randomizedSpeed * transform.forward * Time.fixedDeltaTime; if (moveVector.magnitude <= Vector3.Distance(transform.position, targetPosition)) { transform.position += moveVector; } else { transform.position = targetPosition; nextActionTime = Time.fixedTime; } } }