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; } }
// 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); } }
// 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; } }
void Update() { switch (input) { case Schemes.WASD: if (Input.GetKey(KeyCode.W)) { motor.Move(data.frontSpeed); } if (Input.GetKey(KeyCode.S)) { motor.Move(-data.backSpeed); } if (Input.GetKey(KeyCode.A)) { motor.Turn(-data.turnSpeed); } if (Input.GetKey(KeyCode.D)) { motor.Turn(data.turnSpeed); } break; case Schemes.ARROWS: if (Input.GetKey(KeyCode.UpArrow)) { motor.Move(data.frontSpeed); } if (Input.GetKey(KeyCode.DownArrow)) { motor.Move(-data.backSpeed); } if (Input.GetKey(KeyCode.LeftArrow)) { motor.Turn(-data.turnSpeed); } if (Input.GetKey(KeyCode.RightArrow)) { motor.Turn(data.turnSpeed); } break; default: break; } if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Keypad0)) { if (Time.time > nextShoot) { shoot.Shoot(data.shellForce); nextShoot = Time.time + data.fireRate; } } }
void Update() { switch (scheme) { case InputScheme.WASD: if (Input.GetKey(KeyCode.W)) { motor.Move(data.moveSpeed); } else if (Input.GetKey(KeyCode.S)) { motor.Move(-data.backwardsSpeed); } if (Input.GetKey(KeyCode.A)) { motor.Rotate(-data.turnSpeed); } else if (Input.GetKey(KeyCode.D)) { motor.Rotate(data.turnSpeed); } if (Input.GetKeyDown(KeyCode.Space) && Time.time >= nextFireTime) { motor.Shoot(data.bulletForce); nextFireTime = Time.time + data.fireRate; } break; case InputScheme.ArrowKeys: if (Input.GetKey(KeyCode.UpArrow)) { motor.Move(data.moveSpeed); } if (Input.GetKey(KeyCode.DownArrow)) { motor.Move(-data.backwardsSpeed); } if (Input.GetKey(KeyCode.LeftArrow)) { motor.Rotate(-data.turnSpeed); } if (Input.GetKey(KeyCode.RightArrow)) { motor.Rotate(data.turnSpeed); } if (Input.GetKeyDown(KeyCode.Keypad0) && Time.time >= nextFireTime) { motor.Shoot(data.bulletForce); nextFireTime = Time.time + data.fireRate; } break; } }
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); } } }
//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; } }
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; } }
// 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."); } } }
// 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() { switch (inputScheme) { case InputScheme.WASD: // handling movement if (Input.GetKey(KeyCode.W)) { motor.Move(data.moveSpeed); } else if (Input.GetKey(KeyCode.S)) { motor.Move(-data.moveSpeed); } else { motor.Move(0); } // handling rotation if (Input.GetKey(KeyCode.A)) { motor.Rotate(-data.turnSpeed); } else if (Input.GetKey(KeyCode.D)) { motor.Rotate(data.turnSpeed); } // shooting if (Input.GetKey(KeyCode.Space)) { shooter.Shoot(); } break; case InputScheme.arrowKeys: break; default: Debug.LogError("[InputController] Input scheme not implemented"); break; } }
public void Resting() { aiMotor.TurnTowards(GameObject.FindGameObjectWithTag("Rest").transform.position, aiData.turnSpeed); aiMotor.Move(aiData.frontSpeed); while (aiData.health < aiData.maxHealth) { aiData.health += 1; } if (aiData.health == aiData.maxHealth) { aiMode = defaultMode; } }
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; } }
// Update is called once per frame void Update() { switch (inputScheme) { case InputScheme.WASD: // When pressing W and S, the tank moves Back and Forth if (Input.GetKey(KeyCode.W)) { motor.Move(data.moveSpeed); } else if (Input.GetKey(KeyCode.S)) { motor.Move(-data.moveSpeed); } else { motor.Move(0); } // when pressing A and D key, the tank moves left and right respectively if (Input.GetKey(KeyCode.A)) { motor.Rotate(-data.turnSpeed); } else if (Input.GetKey(KeyCode.D)) { motor.Rotate(data.turnSpeed); } // When space is pressed, shoots the cannonball if (Input.GetKey(KeyCode.Space)) { data.attack.Shoot(data.cannonBallPrefab); } break; case InputScheme.arrowKeys: break; default: Debug.LogError("[InputController] Input scheme not implemented."); break; } }
//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 ChaseAndFire(GameObject target) { motor.RotateTowards(target.transform.position, data.turnSpeed); if (controller.canMove((data.moveSpeed * 0.5f))) { motor.Move(data.moveSpeed * 0.5f); } else { controller.avoidStage = 1; } if (Time.time > lastEventTime + timerDelay) { lastEventTime = Time.time; shooter.shoot(); } }
// 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); } }
// 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; } }
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; } }
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(); } }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.W)) { motor.Move(data.moveSpeed); } if (Input.GetKey(KeyCode.S)) { motor.Move(data.moveSpeed * -1); } if (Input.GetKey(KeyCode.D)) { motor.Rotate(data.rotateSpeed); } if (Input.GetKey(KeyCode.A)) { motor.Rotate(data.rotateSpeed * -1); } }
// Update is called once per frame void Update() { switch (input) { case InputScheme.arrowKeys: if (Input.GetKey(KeyCode.UpArrow)) { motor.Move(data.forwardSpeed); } if (Input.GetKey(KeyCode.DownArrow)) { motor.Move(-data.forwardSpeed); } if (Input.GetKey(KeyCode.RightArrow)) { motor.Rotate(data.turnSpeed); } if (Input.GetKey(KeyCode.LeftArrow)) { motor.Rotate(-data.turnSpeed); } break; case InputScheme.WASD: if (Input.GetKey(KeyCode.W)) { motor.Move(data.forwardSpeed); } if (Input.GetKey(KeyCode.S)) { motor.Move(-data.forwardSpeed); } if (Input.GetKey(KeyCode.D)) { motor.Rotate(data.turnSpeed); } if (Input.GetKey(KeyCode.A)) { motor.Rotate(-data.turnSpeed); } break; } }
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); }
// Update is called once per frame void Update() { if (Input.GetKey(KeyCode.UpArrow)) { motor.Move(data.moveSpeed); } if (Input.GetKey(KeyCode.RightArrow)) { motor.Rotate(data.rotateSpeed); } }
public void Avoid() { if (avoidanceStage == 1) { Debug.Log("Trying to avoid something"); // Tank Rotates Left motor.Rotate(-1 * enemyTurnSpeed); // Tank can move -> Stage 2 if (CanMove(enemyForwardSpeed)) { avoidanceStage = 2; // How long in Stage 2 exitTime = avoidanceTime; } // Otherwise, we'll do this again next turn! } else if (avoidanceStage == 2) { // Move forward if you can if (CanMove(enemyForwardSpeed)) { // Subtract from our timer and move exitTime -= Time.deltaTime; motor.Move(enemyTurnSpeed); // If we have moved long enough, return to chase mode if (exitTime <= 0) { avoidanceStage = 0; } } else { // Otherwise, we can't move forward, so back to stage 1 avoidanceStage = 1; } } }
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 Sentry() { if (CanSeePlayer()) { mode = Mode.Attack; } else { mode = Mode.Patrol; } if (mode == Mode.Patrol) { motor.Move(0); motor.Rotate(data.rotateSpeed); } else if (mode == Mode.Attack) { motor.RotateToward(Player.transform.position, data.rotateSpeed); motor.Fire(); } }