// Update is called once per frame void Update() { if (attackMode == AttackMode.Chase) { //Rotate motor.RotateTowards(target.position, data.rotateSpeed); //move motor.move(data.moveSpeed); } if (attackMode == AttackMode.Flee) { // The vector from ai to target = target position - our position Vector3 vectorToTarget = target.position - tf.position; // flip by -1 Vector3 vectorAwayFromTarget = -1 * vectorToTarget; // normalize vectorAwayFromTarget.Normalize(); vectorAwayFromTarget *= FleeDistance; Vector3 fleePosition = vectorAwayFromTarget + tf.position; motor.RotateTowards(fleePosition, data.rotateSpeed); motor.move(data.moveSpeed); } }
// Update is called once per frame void Update() { switch (attackMode) { case AttackMode.Chase: target = GameObject.Find("Player").GetComponent <Transform>(); //Rotate towards player motor.RotateTowards(target.position, data.rotateSpeed); //Move towards player motor.Move(data.moveSpeed); break; case AttackMode.Flee: // The vector from ai to target is target position minus our position. Vector3 vectorToTarget = target.position - tf.position; // We can flip this vector by -1 to get a vector AWAY from our target Vector3 vectorAway = vectorToTarget * -1; // Now, we can normalize that vector to give it a magnitude of 1 vectorAway.Normalize(); // A normalized vector can be multiplied by a length to make a vector of that length. vectorAway *= fleeDistance; // We can find the position in space we want to move to by adding our vector away from our AI to our AI's position. //This gives us a point that is "that vector away" from our current position. Vector3 fleePosition = vectorAway + tf.position; motor.RotateTowards(fleePosition, data.rotateSpeed); motor.Move(data.moveSpeed); break; default: Debug.LogError("Attack Mode not implemented"); break; } }
// Update is called once per frame void Update() { switch (attackMode) { case AttackMode.Chase: //rotates towards target motor.RotateTowards(target.position, data.rotateSpeed); //move towards target motor.Move(data.moveSpeed); break; case AttackMode.Flee: // The vector from ai to target is target position minus our position. Vector3 vectorToTarget = target.position - tf.position; // We can flip this vector by -1 to get a vector AWAY from our target Vector3 vectorAwayFromTarget = -1 * vectorToTarget; // Now, we can normalize that vector to give it a magnitude of 1 vectorAwayFromTarget.Normalize(); // A normalized vector can be multiplied by a length to make a vector of that length. vectorAwayFromTarget *= fleeDistance; // We can find the position in space we want to move to by adding our vector away from our AI to our AI's position. // This gives us a point that is "that vector away" from our current position. Vector3 fleePosition = vectorAwayFromTarget + tf.position; motor.RotateTowards(fleePosition, data.rotateSpeed); motor.Move(data.moveSpeed); break; default: Debug.LogError(message: "Attack Mode not implemented."); break; } }
private void Flee(GameObject target) { // TODO: write this method // Get the vector to our target Vector3 vectorToTarget = target.transform.position - transform.position; // Get the vector away from our target. Vector3 vectorAwayFromTarget = -1 * vectorToTarget; // Normalize the vector away from the target vectorAwayFromTarget.Normalize(); // Adjust for flee distance vectorAwayFromTarget *= fleeDistance; // Set our flee position Vector3 fleePosition = vectorAwayFromTarget + transform.position; // This way to handle fleeing might make better sense than what is uncommented below //motor.RotateTowards(fleePosition, data.turnSpeed); //motor.Move(data.moveSpeed); if (motor.RotateTowards(fleePosition, data.turnSpeed)) { // Do nothing } else { motor.Move(data.moveSpeed); } }
void DoChase() { // Rotate towards the target motor.RotateTowards(target.position, data.rotateSpeed); if (CanMove(2.0f)) { motor.Move(1.0f); } else { avoidanceStage = 1; } }
// Update is called once per frame void Update() { if (attackMode == AttackMode.Chase) { // Rotate towards our target. motor.RotateTowards(target.position, data.rotateSpeed); // Move towards our target. motor.Move(data.moveSpeed); } if (attackMode == AttackMode.Flee) { Flee(); } }
public void Chase(GameObject target) { if (motor.RotateTowards(target.transform.position, data.turnSpeed)) { // Do Nothing } else { if (Vector3.SqrMagnitude(transform.position - target.transform.position) >= (closeEnough * closeEnough)) { motor.Move(data.moveSpeed); } } }
//different actions state machine void EnemyModeStateMachine() { switch (enemyMode) { case EnemyMode.Chase: Chasing(); break; case EnemyMode.Flee: Fleeing(); break; case EnemyMode.Idle: Idle(); break; case EnemyMode.Search: Search(); break; case EnemyMode.Patrol: //Patrol(); break; case EnemyMode.Hide: Hide(); break; case EnemyMode.Wait: Wait(); break; case EnemyMode.Attack: motor.RotateTowards(target.position, data.rotateSpeed); shoot.ShootBullet(); break; case EnemyMode.Sneak: Sneak(); break; case EnemyMode.Rotate: motor.RotateTowards(target.position, data.rotateSpeed); break; default: Debug.LogWarning("Not an available mode: EnemyPersonality"); break; } }
//Hearing/Seeing private void Sense() { switch (aiState) { case AIState.Patrol: //Always move in a circle in patrol mode motor.Move(data.moveSpeed); motor.Rotate(data.rotateSpeed); //If the player is able to be heard... if (distance <= hearingDistance) { //Change to hear ChangeState(AIState.Hear); } break; case AIState.Hear: //Look at the player motor.RotateTowards(player.GetComponent <Transform>().position, data.moveSpeed); //If the AI sees the player... if (CanMove(data.moveSpeed)) { //Chase the player ChangeState(AIState.Chase); } //Otherwise... else { //Go back to patrol ChangeState(AIState.Patrol); } break; case AIState.Chase: //Chase the player Chase(player); //If the AI no longer sees the player if (!CanMove(data.moveSpeed)) { //If the player is still alive if (player.GetComponent <TankData>().health > 0) { //Go back to patrol ChangeState(AIState.Patrol); } } break; } }
// Update is called once per frame void Update() { if (motor.RotateTowards(waypoints[currentWaypoint].position, data.rotateSpeed)) { // Do nothing } else { motor.Move(1.0f); gameObject.SendMessage("Shoot", SendMessageOptions.DontRequireReceiver); } // it would be better to store transform in a var in Start! if (Vector3.SqrMagnitude(waypoints[currentWaypoint].position - transform.position) <= (closeEnough * closeEnough)) { // dont go outside the array if (currentWaypoint < waypoints.Length - 1) { currentWaypoint++; } else { currentWaypoint = 0; } } }
// Update is called once per frame void Update() { if (motor.RotateTowards(waypoints[currentWaypoint].position, data.rotateSpeed)) { // Do nothing } else { motor.Move(data.moveSpeed); } if (Vector3.SqrMagnitude(waypoints[currentWaypoint].position - tf.position) < (closeEnough * closeEnough)) { if (loopType == LoopType.Stop) { StopLoop(); } else if (loopType == LoopType.PingPong) { PingPongLoop(); } else if (loopType == LoopType.Loop) { LoopLoop(); } else { Debug.LogWarning("[SampleAIController] Unimplemented loop type."); } } }
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; } }
void DoChase() { motor.RotateTowards(Target.position, data.rotateSpeed); // checks if we can move 'data.movespeed' units away if (Vector3.SqrMagnitude(Target.position - tf.position) < ((closeEnough + 4) * (closeEnough + 4))) // causes tank to stop when close enough to player { // do nothing } else if (CanMove(WallStop)) { motor.move(data.moveSpeed); } else { avoidanceStage = 1; } }
// Update is called once per frame void Update() { //Switch behavior based on current Attack Mode switch (attackMode) { case AttackMode.Chase: //Rotate towards the target at rotate speed (note: only rotates to the right! use neg. speed to rotate left) motor.RotateTowards(target.position, data.rotateSpeed); //move towards target motor.Move(data.moveSpeed); break; case AttackMode.Flee: //The vector from enemy to target is the difference of target pos. minus our pos. Vector3 vectorToTarget = target.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); //move forward motor.Move(data.moveSpeed); break; default: Debug.Log("Attack mode not implemented"); break; } }
// Update is called once per frame void Update() { if (attackMode == AttackMode.Chase) { motor.RotateTowards(targetTf.position, enemyForwardSpeed); motor.Move(enemyForwardSpeed); } if (attackMode == AttackMode.Flee) { Vector3 vectorAwayFromTarget = (targetTf.position - tf.position) * -1; vectorAwayFromTarget.Normalize(); vectorAwayFromTarget *= fleeDistance; Vector3 fleePosition = vectorAwayFromTarget + tf.position; motor.RotateTowards(fleePosition, enemyTurnSpeed); motor.Move(enemyForwardSpeed); } }
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(); } }
private void Flee() { Vector3 vectorToTarget = target.position - tf.position; Vector3 vectorAwayFromTarget = -vectorToTarget; vectorAwayFromTarget.Normalize(); Vector3 fleePosition = vectorAwayFromTarget + tf.position; motor.RotateTowards(fleePosition, data.rotateSpeed); motor.Move(data.moveSpeed); }
void Flee() { //Vector from Target to this gameObject Vector3 vectorToTarget = target.position - tf.position; //Make it the opposite direction of the target Vector3 vectorAwayFromTarget = vectorToTarget * -1; //Make it 1 unit in length Vector3.Normalize(vectorAwayFromTarget); //Flee to the distance we chose vectorAwayFromTarget *= fleeDistance; //The postion the AI will move to Vector3 fleePositon = tf.position + vectorAwayFromTarget; //Rotate towards the position and move to it motor.RotateTowards(fleePositon); motor.Move(tank.transform.forward, tank.forwardSpeed); }
private void DoChase() { motor.RotateTowards(target.position, data.rotateSpeed); if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } else { avoidanceStage = AvoidanceStage.Rotate; } }
void DoChase() { motor.RotateTowards(target.position, data.rotateSpeed); // checks if we can move 'data.movespeed' units away if (CanMove(WallStop)) { motor.move(data.moveSpeed); } else { avoidanceStage = 1; } }
public void Chase(GameObject target) { if (motor.RotateTowards(target.transform.position, data.turnSpeed)) { //Do Nothing } else { if (Vector3.SqrMagnitude(transform.position - target.transform.position) >= (closeEnough * closeEnough)) { motor.Move(data.moveSpeed); } else { if (Time.time > lastEventTime + timerDelay) { lastEventTime = Time.time; shooter.shoot(); } } } }
protected void patrol() { if (avoidance.CurrentAvoidanceState == ObstacleAvoidance.Avoidance.none) { motor.Move(data.moveSpeed); motor.RotateTowards(WayPoint[CurrentWaypoint].position); } //Check if we are close enough to the current waypoint if (Vector3.Distance(transform.position, WayPoint[CurrentWaypoint].position) < 1) { //If we are, switch to the next waypoint //if our current waypoint is greater than or equal to waypoint.size if (CurrentWaypoint >= WayPoint.Count) { //then set current waypoint = 0 CurrentWaypoint = 0; } //if not, increment current waypoint by 1 else { CurrentWaypoint += 1; } } }
public void Follow() { Debug.Log("Following!"); motor.RotateTowards(target.position, enemyTurnSpeed); if (CanMove(enemyForwardSpeed)) { Debug.Log("I can follow something!"); motor.Move(enemyForwardSpeed); } else { // Enter obstacle avoidance stage 1 avoidanceStage = 1; } }
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; } }
//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; } }
void Chase() { if (CanMove(data.moveSpeed)) { //move if can move if (motor.RotateTowards(target.position, data.rotateSpeed)) { // Do nothing } else { motor.Move(data.moveSpeed); } } else { avoidanceStage = AvoidStage.rotateUntilCanMove; } }
// Update is called once per frame void Update() { //If we're not at the waypoint, turn to face it if (motor.RotateTowards(waypoints[currentWaypoint].transform.position, data.turnSpeed)) { //Do nothing } //If we're facing the waypoint, move towards it else { motor.Move(data.moveSpeed); } //If we've arrived at our waypoint, then go to the next one if (loopType == LoopType.Stop) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (currentWaypoint < (waypoints.Length - 1)) { currentWaypoint++; } } } else if (loopType == LoopType.Loop) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (currentWaypoint < (waypoints.Length - 1)) { currentWaypoint++; } else { currentWaypoint = 0; } } } }
//Called once per frame MAIN SCRIPT LOOP void Update() { //If player doesn't exist set tanks back to attackMode.Neutral if (playerTf == null) { attackMode = AttackMode.Neutral; } //if data.heatlh < 49 enter flight attackmode if (data.health <= 49) { attackMode = AttackMode.Flight; } // // Handle Player state // //Neutral if (attackMode == AttackMode.Neutral) { //if avoidace stage != 0 then we are in avoidance mode if (avoidanceStage != 0) { DoAvoidance(); } else { //we are not in avoidance mode //If we can move forward by data.moveSpeed units if (CanMove(data.moveSpeed)) { if (motor.RotateTowards(waypoints [currentWaypoint].transform.position, data.rotateSpeed)) { //Do Nothing we are looking at the current waypoint } else { //Move Speed motor.Move(data.moveSpeed); } //If the distance between us and the current waypoint < closeEnough if (Vector3.Distance(tf.position, waypoints [currentWaypoint].transform.position) < closeEnough) { //If TheStickler if (personality == EnemyPersonallity.TheStickler) { //increment currentWaypoint or set it back to zero currentWaypoint++; if (currentWaypoint == waypoints.Length) { currentWaypoint = 0; } } else if (personality == EnemyPersonallity.TheCreep) { currentWaypoint = GetClosestWayPoint(playerTf); } else if (personality == EnemyPersonallity.TheLooseCannon) { //GetRandomIndex currentWaypoint = Random.Range(0, waypoints.Length); } else if (personality == EnemyPersonallity.TheStickInTheMud) { //if the currentWayPoint == looseCannonStartWayPoint if (looseCannonStartWayPoint == currentWaypoint) { currentWaypoint++; } else //else set it back to startingWaypoint { currentWaypoint = looseCannonStartWayPoint; } } } } else { avoidanceStage = 1; } } } // //Fight // else if (attackMode == AttackMode.Fight) { //Look for player and see if he is in line of sight - if Player exists that is :) if (playerTf != null) { if (avoidanceStage != 0) { DoAvoidance(); } else { //Try and get the enemy tank between 30 and 60 units from player for optimal aim if (Vector3.Distance(tf.position, playerTf.position) > 60) { if (CanMove(data.moveSpeed)) { motor.Move(data.moveSpeed); } else { avoidanceStage = 1; } } else if (Vector3.Distance(tf.position, playerTf.position) < 30) { if (CanMove(data.moveSpeed)) { motor.Move(-data.reverseSpeed); } } RaycastHit hit; Debug.DrawRay(tf.position, playerTf.position - tf.position, Color.grey); if (Physics.Raycast(tf.position, playerTf.position - tf.position, out hit)) { if (hit.collider.gameObject.tag != "Player") { //Do Nothing the player is not in sight } else { //Player is in sight motor.RotateTowards(playerTf.position, data.moveSpeed); if (gun.isLoaded) { gun.Fire(); } } } } } } // //Flight // else if (attackMode == AttackMode.Flight) { if (avoidanceStage != 0) { DoAvoidance(); } else { //The vector to target minus the tf.position Vector3 vectorToTarget = playerTf.position - tf.position; //Flip Away From Target Vector3 vectorAwayFromTarget = -1 * vectorToTarget; //Normalize Vector3 vectorAwayFromTarget.Normalize(); //Get the distance vectorAwayFromTarget *= fleeDistance; Vector3 fleePosition = vectorAwayFromTarget + tf.position; if (CanMove(data.moveSpeed)) { if (motor.RotateTowards(fleePosition, data.rotateSpeed)) { } else { motor.Move(data.moveSpeed); } } else { avoidanceStage = 1; } } } }
// Update is called once per frame void Update() { // if we're not rotated to face the waypoint, turn to face it. if (motor.RotateTowards(waypoints[currentWaypoint].transform.position, data.turnSpeed)) { // Do nothing } // if we're facing the waypoint, move towards it. else { motor.Move(data.moveSpeed); } // If we've arrived at our waypoint, then go to the next one. if (loopType == LoopType.Stop) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (IsNotAtFinalWaypointProperty) { currentWaypoint++; } } } else if (loopType == LoopType.Loop) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (IsNotAtFinalWaypoint()) { currentWaypoint++; } else { currentWaypoint = 0; } } } else if (loopType == LoopType.PingPong) { if (isLoopingForward) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (IsNotAtFinalWaypoint()) { currentWaypoint++; } else { isLoopingForward = false; } } } else { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (currentWaypoint > 0) { currentWaypoint--; } else { isLoopingForward = true; } } } } else { Debug.LogWarning("[SampleAIController1] Unexepected LoopType"); } }
private void WandererFSM() { // We need to see if we are already at the waypoint. // If we are not at the waypoint, turn to face it. if (motor.RotateTowards(waypoints[currentWaypoint].transform.position, data.turnSpeed)) { // Do nothing! } // If we are facing the waypoint, move towards it. // Move forward. motor.Move(data.moveSpeed); // If we've arrived at our waypoint, then go to the next one. if (loopType == LoopType.Stop) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (currentWaypoint < (waypoints.Length - 1)) { currentWaypoint++; } } } else if (loopType == LoopType.Loop) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (currentWaypoint < (waypoints.Length - 1)) { currentWaypoint++; } else { currentWaypoint = 0; } } } else if (loopType == LoopType.PingPong) { if (isLoopingForward) { if (Vector3.SqrMagnitude(transform.position - waypoints[currentWaypoint].transform.position) <= (closeEnough * closeEnough)) { if (currentWaypoint < (waypoints.Length - 1)) { currentWaypoint++; } else { isLoopingForward = false; } } } // TODO: Write own behavior } }