Soccer Ai: Switching between Defensive and Offensive behavior

Hey, I am currently working on a Space Soccer Game and their Ai at the moment. You can check out my current progress here: http://pixelpizza.de/unity/football_ai.html the controls are A and D to move and W for a special attack.

as you can see, the Drone is just following the Ball but i want to switch between a Defensive and Offensive behavior. so that the Drone is pushing the Ball to the Goal and also moving back to his own goal when the player is near the own goal. the Ai code looks like this atm:

using UnityEngine;
using System.Collections;

public class DroneBehavior : MonoBehaviour {
	
	private GameObject player;
	
	[SerializeField]
	private float moveSpeed = 250;
	[SerializeField]
	private float minDistance = 50.0f;
	[SerializeField]
	private float maxDistance = 100.0f;
	[SerializeField]
	private float rotationDrag = 0.75f;
	[SerializeField]
	private bool canShoot = true;
	[SerializeField]
	private float brakeForce = 5f;
	
	private bool isShooting = false;
	private Vector3 direction;
	private float distance = 0.0f;
	
	public enum CurrentState { Idle, Following, Attacking };
	public CurrentState currentState;
	public bool debugGizmo = true;
	
	public float DistanceToPlayer { get { return distance; } }
	public bool CanShoot { get { return canShoot; } set { canShoot = value; } }
	
	
	
	void Start()
	{  
		player = GameObject.FindGameObjectWithTag("Ball");
		currentState = CurrentState.Idle;
		isShooting = false;
	}
	
	void Update() 
	{
		//Find the distance to the player
		distance = Vector3.Distance(player.transform.position, this.transform.position);
		
		//Face the drone to the player
		direction = (player.transform.position - this.transform.position);
		direction.Normalize();
	}
	
	private void FixedUpdate()
	{  
		this.GetComponent<Rigidbody>().rotation = Quaternion.LookRotation(direction, Vector3.up);
		this.GetComponent<Rigidbody>().angularDrag = rotationDrag;
		
		//If the player is in range move towards
		if(distance > minDistance && distance < maxDistance )
		{
			currentState = CurrentState.Following;
			DroneMovesToPlayer();
		}
		
		//If the player is close enough shoot
		else if(distance < minDistance)
		{
			
			DroneStopsMoving();
			
			if(canShoot)
			{
				currentState = CurrentState.Attacking;
				ShootPlayer();
			}
		}
		
		//If the player is out of range stop moving
		else
		{
			currentState = CurrentState.Idle;
			DroneStopsMoving();
		}
	}
	
	private void DroneStopsMoving()
	{ 
		isShooting = false;
		this.GetComponent<Rigidbody>().drag = (brakeForce);
	}
	
	private void DroneMovesToPlayer()
	{
		isShooting = false;
		//this.GetComponent<Rigidbody>().AddForce(-200,200,0);
		this.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * moveSpeed);
	}
	
	private void ShootPlayer()
	{
		isShooting = true;
		//Shoot player ...
	}
	
	private void OnDrawGizmosSelected()
	{
		if (debugGizmo)
		{
			Gizmos.color = Color.green;
			Gizmos.DrawWireSphere(this.transform.position, maxDistance);
			
			Gizmos.color = Color.red;
			Gizmos.DrawWireSphere(this.transform.position, minDistance);
		}
	}
} 

maybe someone has a great idea how to do this :slight_smile: thanks!

Well…I’d do everything completely differently but I’ll assume you don’t want to change everything.

Simplest solution is, essentially, to simply make an enum or something to differentiate between your current ‘tactic’, so just like

public enum Tactics {Offensive, Defensive};

then, in wherever your AI decides what to do, first you make a check

Tactics currentTactic;


public void AIUpdate(){

 switch(currentTactic){
  case Tactics.Offensive:
       OffensiveAction();
       break;
   case Tactics.Defensive:
      DefensiveAction();
      break;
 }
}

void OffensiveAction(){

//put offensive action code here
//be sure to put in some statement that checks if maybe we should change tactics
//same for defense

}

Of course what your AI should actually do when it is taking offensive or defensive actions is up to you.