Fixed major AI issues

Tweaked AI parameters
Improved AI behaviour to avoid getting stuck near walls
Changed boost conditions
This commit is contained in:
2020-01-31 05:23:35 +01:00
parent 238187c30a
commit dd3816ce0b
4 changed files with 244 additions and 30 deletions

View File

@@ -9,14 +9,16 @@ public class AIController : MonoBehaviour
public Vector3 inputVector;
public float turnRadius = 1;
public float destinationOffset;
public float maxBoostDist = 3;
public float distanceToTarget;
public float distanceToTarget; //Distance to the point, placed behind the ball
public float angleToDestination;
public float angleToBall;
public float distanceToBall;
private Vector3 destination;
private Vector3 directionVector;
private float rotationTimeout = 0;
// Start is called before the first frame update
void Start()
{
@@ -53,8 +55,9 @@ public class AIController : MonoBehaviour
Debug.DrawLine(this.transform.position, this.destination, Color.yellow, 0f, false);
Debug.DrawLine(this.targetGoal.position, this.destination, Color.yellow, 0f, false);
Gizmos.DrawSphere(this.destination, 1);
Debug.DrawRay(transform.position, this.transform.forward * 3.8f, Color.white, .5f);
Debug.DrawRay(transform.position, this.transform.forward * 10, Color.green);
Debug.DrawRay(transform.position, this.directionVector.normalized * 10, Color.red);
}
@@ -66,36 +69,40 @@ public class AIController : MonoBehaviour
inputs[AiInputs.VERTICAL] = 0;
inputs[AiInputs.BOOST] = 0;
//Move forward if we are too far away
if (this.distanceToTarget > 4)
{
inputs[AiInputs.VERTICAL] = 1;
}
Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.forward) * 8, Color.magenta, 0, false);
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(this.angleToDestination * 10) / 10;
if (this.distanceToTarget < this.turnRadius)
RaycastHit hit;
//If we hit something head on, stop, drop and rotate (emergency rotate)
if (Physics.Raycast(transform.position, this.transform.forward, out hit, 3.8f))
{
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(-this.angleToBall * 10) / 10;
if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.forward), 8))
{
inputs[AiInputs.BOOST] = 1;
}
this.rotationTimeout = .2f;
}
//Rotate if we are close enough to the target (behind the ball) but not if emergency rotation is active
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(this.angleToDestination * 10) / 10;
if (this.distanceToTarget < this.turnRadius && this.rotationTimeout < 0)
{
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(-this.angleToBall * 10) / 10;
}
this.rotationTimeout -= Time.deltaTime;
if(rotationTimeout > 0)
{
inputs[AiInputs.HORIZONTAL] = .5f;
}
if (this.distanceToBall < this.turnRadius)
//Shoot a ray backwards and boost if we are close enough to the target (behind the ball) to avoid shooting towards our own goal
if (Physics.Raycast(transform.position, -this.transform.forward, out hit, 15))
{
RaycastHit hit;
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(-this.angleToBall * 10) / 10;
if (Physics.Raycast(transform.position, this.directionVector, out hit, 5))
if (hit.transform.tag == "Ball" && this.distanceToTarget < this.maxBoostDist)
{
if(hit.transform.tag == "Ball")
{
inputs[AiInputs.BOOST] = 1;
}
inputs[AiInputs.BOOST] = 1;
}
}