void Avoid() { if (avoidanceStage == AvoidanceStage.Turn) //if turning { motor.Rotate(-1 * data.turnSpeed); //turn to the left if (CanMove(data.moveSpeed * Time.deltaTime)) //if can move at current rotation { avoidanceStage = AvoidanceStage.Advance; //start advancing exitTime = avoidanceTime; //set time to stay in advancing stage } } else if (avoidanceStage == AvoidanceStage.Advance) //if advancing { if (CanMove(data.moveSpeed)) //if can move { exitTime -= Time.deltaTime; //update time motor.Move(data.moveSpeed); //move forward if (exitTime <= 0) { avoidanceStage = AvoidanceStage.Default; //if time is up, go back to default avoidance } } else { avoidanceStage = AvoidanceStage.Turn; //if can't move, go back to turning } } }
private void DoAvoid() { switch (avoidanceStage) { case AvoidanceStage.Rotate: motor.Rotate(data.rotateSpeed); if (CanMove(data.moveSpeed)) { avoidanceStage = AvoidanceStage.Move; exitTime = avoidanceTime; } break; case AvoidanceStage.Move: if (CanMove(data.rotateSpeed)) { exitTime -= Time.deltaTime; motor.Move(data.moveSpeed); if (exitTime <= 0f) { avoidanceStage = AvoidanceStage.None; } } else { avoidanceStage = AvoidanceStage.Rotate; } break; } }
//Flee from a target object [HAS OBSTACLE AVOIDANCE] private void Flee(GameObject target) { //The vector from enemy to target is the difference of target pos. minus our pos. Vector3 vectorToTarget = target.transform.position - tf.position; //Get vector away from target (Flip by -1) Vector3 vectorAwayFromTarget = -1 * vectorToTarget; //Normalize away vector (sets to a magnitude of 1) vectorAwayFromTarget.Normalize(); //Multiply direction to run (vectorAway) by how far away to run (flee distance) vectorAwayFromTarget *= fleeDistance; //Locate flee location point in space relative to our position to seek Vector3 fleePosition = vectorAwayFromTarget + tf.position; //Rotate towards the flee position motor.RotateTowards(fleePosition, data.rotateSpeed); //If I can move, move forward if (CanMove(data.feelDistance)) { motor.Move(data.moveSpeed); } //Else I can't move, avoid the obstacle else { //Rotate until we can move avoidanceStage = AvoidanceStage.Rotate; } }
private void Avoid() { if (avoidanceStage == AvoidanceStage.Rotate) { motor.Rotate(data.rotateSpeed); if (CanMove(data.moveSpeed)) { avoidanceStage = AvoidanceStage.Move; exitTime = avoidanceTime; } } if (avoidanceStage == AvoidanceStage.Move) { if (CanMove(data.moveSpeed)) { exitTime -= Time.deltaTime; motor.Move(data.moveSpeed); if (exitTime <= 0.0f) { avoidanceStage = AvoidanceStage.None; } } else { avoidanceStage = AvoidanceStage.Rotate; } } }
void Chase(Vector3 tar) { motor.RotateTowards(tar, data.turnSpeed);//turn towards target if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); //if can move, then move } else { avoidanceStage = AvoidanceStage.Turn; //otherwise start avoidance } }
//Avoid whatever obstacle is in the way using Avoidance FSM private void Avoid() { //FSM of avoidance states switch (avoidanceStage) { //---NO AVOIDANCE STATE--- case AvoidanceStage.None: //Do nothing break; //---ROTATE TO AVOID STATE--- case AvoidanceStage.Rotate: //Begin rotating motor.Rotate(data.rotateSpeed); //causes the tank to turn to the right each time //If can move forward, change avoidance stage to Move if (CanMove(data.moveSpeed)) { //change avoidance stage to Move avoidanceStage = AvoidanceStage.Move; //Set exitTime exitTime = avoidanceTime; } break; //---MOVE TO AVOID STATE--- case AvoidanceStage.Move: //If we can move, move until end of timer if (CanMove(data.moveSpeed)) { exitTime -= Time.deltaTime; //deduct time each frame motor.Move(data.moveSpeed); //move the tank forward //If timer has expired if (exitTime <= 0) { //Change avoidance stage to none avoidanceStage = AvoidanceStage.None; } } //Else we can't move forward else { //Change to avoidance stage Rotate avoidanceStage = AvoidanceStage.Rotate; } break; } }
private void DoChase() { motor.RotateTowards(target.position, data.rotateSpeed); if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } else { avoidanceStage = AvoidanceStage.Rotate; } }
public void Chase(GameObject target) { target = GameObject.FindWithTag("Player"); //Rotate towards player motor.RotateTowards(target.GetComponent <Transform>().position, data.rotateSpeed); if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } else { avoidanceStage = AvoidanceStage.Rotate; } }
private void Chase() { motor.RotateTowards(target.position, data.rotateSpeed); //only move if we can. if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } else { //rotate until we can move avoidanceStage = AvoidanceStage.Rotate; } }
private void Chase(GameObject targetGameObject) { //TODO:Intergate obstcale avoidence motor.RotateTowards(target.position, data.rotateSpeed); //only move if we can. if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } else { //rotate until we can move avoidanceStage = AvoidanceStage.Rotate; } }
//Chase a target position private void Chase() { //Face my target motor.RotateTowards(target.position, data.rotateSpeed); //If I can move, move forward if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } //Else I can't move, avoid the obstacle else { //Rotate until we can move avoidanceStage = AvoidanceStage.Rotate; } }
public void Chase(GameObject target) { if (motor.RotateTowards(target.transform.position, data.turnSpeed)) { //do nothing } else if (!CanMove(data.moveSpeed)) { avoidanceStage = AvoidanceStage.ObstacleDetected; } else { if (Vector3.SqrMagnitude(transform.position - target.transform.position) >= (closeEnough * closeEnough)) { motor.Move(data.moveSpeed); } shooter.Shoot(); } }
public void Avoid() { if (avoidanceStage == AvoidanceStage.ObstacleDetected) { // Rotate left motor.Rotate(-1 * data.turnSpeed); // If can move forward, go to stage 2 if (CanMove(data.moveSpeed)) { avoidanceStage = AvoidanceStage.AvoidingObstacle; // Set number of seconds in timer before next stage exitTime = avoidanceTime; } } else if (avoidanceStage == AvoidanceStage.AvoidingObstacle) { // If can move forward, do so if (CanMove(data.moveSpeed)) { // Subtract from our timer and mover exitTime -= Time.deltaTime; motor.Move(data.moveSpeed); // If we have moved long enough, return to chase mode if (exitTime <= 0) { avoidanceStage = AvoidanceStage.NotAvoiding; } } else { avoidanceStage = AvoidanceStage.ObstacleDetected; } } // Turn to go around the obstacle // Move forward for a period of time // Attempt to go towards our target again }
public void Avoid() { if (avoidanceStage == AvoidanceStage.ObstacleDetected) { // rotate left motor.Rotate(-1 * data.turnSpeed); // if can move forward move to stage 2 if (CanMove(data.moveSpeed)) { avoidanceStage = AvoidanceStage.AvoidingObstacle; //secods delayed in stage 2 exitTime = avoidanceTime; } } else if (avoidanceStage == AvoidanceStage.AvoidingObstacle) { // if I can move forward do so if (CanMove(data.moveSpeed)) { //subtract from timer exitTime -= Time.deltaTime; motor.Move(data.moveSpeed); //if we have moved long enough return to chase mode if (exitTime <= 0) { avoidanceStage = AvoidanceStage.NotAvoiding; } } else { avoidanceStage = AvoidanceStage.ObstacleDetected; } } //attempt to go towards our target again }
private void Avoid() { switch (avoidanceStage) { case AvoidanceStage.Rotate: //Rotate motor.Rotate(data.rotateSpeed); //If there is nothing in the way... if (CanMove(data.moveSpeed)) { //Move avoidanceStage = AvoidanceStage.Move; exitTime = avoidanceTime; } break; case AvoidanceStage.Move: //If there is nothing in the way... if (CanMove(data.rotateSpeed)) { exitTime -= Time.deltaTime; //Move forwards motor.Move(data.moveSpeed); if (exitTime <= 0f) { avoidanceStage = AvoidanceStage.None; } } //Otherwise... else { //Rotate avoidanceStage = AvoidanceStage.Rotate; } break; } }
public void Avoid() { if (avoidanceStage == AvoidanceStage.ObstacleDetected) { // Rotate left motor.Rotate(-1 * data.turnSpeed); // If I can now move forward, move to stage 2! if (CanMove(data.moveSpeed)) { avoidanceStage = AvoidanceStage.AvoidingObstacle; // Set the number of seconds we will stay in Stage2 exitTime = avoidanceTime; } } else if (avoidanceStage == AvoidanceStage.AvoidingObstacle) { // if we can move forward, do so if (CanMove(data.moveSpeed)) { // Subtract from our timer and move exitTime -= Time.deltaTime; motor.Move(data.moveSpeed); // If we have moved long enough, return to chase mode if (exitTime <= 0) { avoidanceStage = AvoidanceStage.NotAvoiding; } } else { avoidanceStage = AvoidanceStage.ObstacleDetected; } } }
//Patrol a set of waypoints [HAS OBSTACLE AVOIDANCE] public void Patrol() { //If currently turning to look at waypoint if (motor.RotateTowards(waypoints[currentWayPoint].position, data.rotateSpeed)) { //Do Nothing! Do not interrupt the rotating towards waypoint. } //Try to move forward or enter avoidance else { //If I can move, move forward if (CanMove(data.feelDistance)) { motor.Move(data.moveSpeed); } //Else I can't move, avoid the obstacle else { //Rotate until we can move avoidanceStage = AvoidanceStage.Rotate; } } //If close enough to the waypoint, progress onward. if (Vector3.SqrMagnitude(waypoints[currentWayPoint].position - tf.position) < (closeEnough * closeEnough)) //Note: I think there may be issues with using SqrMagnitude, as the product of two decimals is smaller than its factors. Use numbers greater than 1 { switch (loopType) { case LoopType.Stop: //if next waypoint will not exceed the array (avoid index out of range) if (currentWayPoint + 1 < waypoints.Length) { //Target next waypoint currentWayPoint++; } break; case LoopType.Loop: //if next waypoint will not exceed the array (avoid index out of range) if (currentWayPoint + 1 < waypoints.Length) { //Target next waypoint currentWayPoint++; } //else, we are at end of waypoint list else { currentWayPoint = 0; // return to first waypoint } break; case LoopType.Pingpong: //PingPong Forward if (isPingpongForward == true) { //if next waypoint will not exceed the array (avoid index out of range) if (currentWayPoint + 1 < waypoints.Length) { //Target next waypoint currentWayPoint++; } //else, we are at end of waypoint list -> pingpong backwards now else { isPingpongForward = false; //if loop currentWayPoint--; // return to first waypoint } } //PingPong Backwards else { //if next waypoint will not exceed the array beginning(avoid index out of range) if (currentWayPoint - 1 >= 0) { //Target next reverse waypoint currentWayPoint--; } //else, we are at start of waypoint list -> pingpong forwards now else { isPingpongForward = true; //if loop currentWayPoint++; // return to first waypoint } } break; default: Debug.Log("Loop type not implemented."); break; } } }