Add AI Controller
This commit is contained in:
File diff suppressed because it is too large
Load Diff
112
Assets/Scripts/AIController.cs
Normal file
112
Assets/Scripts/AIController.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class AIController : MonoBehaviour
|
||||||
|
{
|
||||||
|
public GameObject ball;
|
||||||
|
public Transform targetGoal;
|
||||||
|
public Vector3 inputVector;
|
||||||
|
public float turnRadius = 1;
|
||||||
|
public float destinationOffset;
|
||||||
|
|
||||||
|
public float distanceToTarget;
|
||||||
|
public float angleToDestination;
|
||||||
|
public float angleToBall;
|
||||||
|
public float distanceToBall;
|
||||||
|
|
||||||
|
private Vector3 destination;
|
||||||
|
private Vector3 directionVector;
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
Vector3 ballPosition = this.ball.transform.position;
|
||||||
|
Vector3 goalDirection = ballPosition - this.targetGoal.position;
|
||||||
|
this.destination = ballPosition + goalDirection.normalized * this.destinationOffset;
|
||||||
|
this.directionVector = this.destination - this.transform.position;
|
||||||
|
|
||||||
|
//Use "right" to offset the angle by 90 deg. This gives us a positive/negative value
|
||||||
|
// depending on the direction we have to rotate
|
||||||
|
this.angleToDestination = Vector3.Dot(this.transform.right, this.directionVector.normalized);
|
||||||
|
|
||||||
|
this.angleToBall = Mathf.Acos(Vector3.Dot((ballPosition - this.transform.position).normalized, this.transform.forward)) * Mathf.Rad2Deg;
|
||||||
|
this.angleToBall = this.Angle360(transform.position, ballPosition, Vector3.right);
|
||||||
|
this.distanceToTarget = Vector3.Distance(this.transform.position, this.destination);
|
||||||
|
this.distanceToBall = Vector3.Distance(this.transform.position, ballPosition);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
float Angle360(Vector3 from, Vector3 to, Vector3 right)
|
||||||
|
{
|
||||||
|
float angle = Vector3.Angle(from, to);
|
||||||
|
return (Vector3.Angle(right, to) > 90f) ? 360f - angle : angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDrawGizmos()
|
||||||
|
{
|
||||||
|
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 * 10, Color.green);
|
||||||
|
Debug.DrawRay(transform.position, this.directionVector.normalized * 10, Color.red);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public float[] GetInputs()
|
||||||
|
{
|
||||||
|
float[] inputs = new float[3];
|
||||||
|
|
||||||
|
inputs[AiInputs.VERTICAL] = 0;
|
||||||
|
inputs[AiInputs.BOOST] = 0;
|
||||||
|
|
||||||
|
if (this.distanceToTarget > 4)
|
||||||
|
{
|
||||||
|
inputs[AiInputs.VERTICAL] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(this.angleToDestination * 10) / 10;
|
||||||
|
if (this.distanceToTarget < this.turnRadius)
|
||||||
|
{
|
||||||
|
inputs[AiInputs.HORIZONTAL] = Mathf.Floor(-this.angleToBall * 10) / 10;
|
||||||
|
Debug.DrawRay(transform.position, transform.TransformDirection(-Vector3.forward) * 10, Color.magenta, 0,false);
|
||||||
|
if (Physics.Raycast(transform.position, transform.TransformDirection(-Vector3.forward), 10))
|
||||||
|
{
|
||||||
|
inputs[AiInputs.BOOST] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.distanceToBall < this.turnRadius)
|
||||||
|
{
|
||||||
|
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")
|
||||||
|
{
|
||||||
|
inputs[AiInputs.BOOST] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.inputVector = new Vector3(inputs[AiInputs.HORIZONTAL], inputs[AiInputs.VERTICAL], inputs[AiInputs.BOOST]);
|
||||||
|
//Left/Right, Fwd/Bkwd, Boost
|
||||||
|
return inputs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class AiInputs
|
||||||
|
{
|
||||||
|
public const int HORIZONTAL = 0;
|
||||||
|
public const int VERTICAL = 1;
|
||||||
|
public const int BOOST = 2;
|
||||||
|
}
|
||||||
11
Assets/Scripts/AIController.cs.meta
Normal file
11
Assets/Scripts/AIController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: dbe2243f96310e94a8da761b45209311
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -45,8 +45,8 @@ public class GameManager : MonoBehaviour
|
|||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
Debug.Log("Calculating matrix: Vector(" + Time.deltaTime * Random.Range(12, 123) + ")");
|
//Debug.Log("Calculating matrix: Vector(" + Time.deltaTime * Random.Range(12, 123) + ")");
|
||||||
Debug.Log("Hacking pentagon: in pr0gress...");
|
//Debug.Log("Hacking pentagon: in pr0gress...");
|
||||||
this.scoreLabel.text = "Score: " + this.scorePlayer1 + " : " + this.scorePlayer2;
|
this.scoreLabel.text = "Score: " + this.scorePlayer1 + " : " + this.scorePlayer2;
|
||||||
|
|
||||||
if (this.scorePlayer1 >= maxGoals)
|
if (this.scorePlayer1 >= maxGoals)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class PlayerController : MonoBehaviour
|
public class PlayerController : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
public bool aiControlled = false;
|
||||||
public string playerNumber;
|
public string playerNumber;
|
||||||
public bool useController = false;
|
public bool useController = false;
|
||||||
public float rotationSpeed = 100;
|
public float rotationSpeed = 100;
|
||||||
@@ -18,12 +19,16 @@ public class PlayerController : MonoBehaviour
|
|||||||
public Vector2 pitchModifier;
|
public Vector2 pitchModifier;
|
||||||
|
|
||||||
private AudioSource audioSource;
|
private AudioSource audioSource;
|
||||||
|
private AIController aiController;
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
this.audioSource = this.forceZone.transform.GetComponent<AudioSource>();
|
this.audioSource = this.forceZone.transform.GetComponent<AudioSource>();
|
||||||
this.rb = GetComponent<Rigidbody>();
|
this.rb = GetComponent<Rigidbody>();
|
||||||
|
if (this.aiControlled)
|
||||||
|
{
|
||||||
|
this.aiController = GetComponent<AIController>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
@@ -31,6 +36,15 @@ public class PlayerController : MonoBehaviour
|
|||||||
{
|
{
|
||||||
float rotationDirection = Input.GetAxis("Horizontal" + this.playerNumber);
|
float rotationDirection = Input.GetAxis("Horizontal" + this.playerNumber);
|
||||||
float movementDirection = Input.GetAxis("Vertical" + this.playerNumber);
|
float movementDirection = Input.GetAxis("Vertical" + this.playerNumber);
|
||||||
|
float[] aiInput = new float[3];
|
||||||
|
|
||||||
|
//Override any inputs if this instance is to be controlled by AI
|
||||||
|
if (this.aiControlled)
|
||||||
|
{
|
||||||
|
aiInput = this.aiController.GetInputs();
|
||||||
|
rotationDirection = aiInput[AiInputs.HORIZONTAL];
|
||||||
|
movementDirection = aiInput[AiInputs.VERTICAL];
|
||||||
|
}
|
||||||
|
|
||||||
if (this.useController) {
|
if (this.useController) {
|
||||||
//Debug.Log(Input.GetAxis("VerticalBack" + this.playerNumber));
|
//Debug.Log(Input.GetAxis("VerticalBack" + this.playerNumber));
|
||||||
@@ -62,7 +76,7 @@ public class PlayerController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Boost
|
//Boost
|
||||||
if (Input.GetButtonDown("Boost" + this.playerNumber) && !this.lockBoost)
|
if (Input.GetButtonDown("Boost" + this.playerNumber) || aiInput[AiInputs.BOOST] > 0 && !this.lockBoost)
|
||||||
{
|
{
|
||||||
this.audioSource.clip = this.boostSound;
|
this.audioSource.clip = this.boostSound;
|
||||||
this.audioSource.pitch = Random.Range(this.pitchModifier.x, this.pitchModifier.y);
|
this.audioSource.pitch = Random.Range(this.pitchModifier.x, this.pitchModifier.y);
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
--- !u!55 &1
|
--- !u!55 &1
|
||||||
PhysicsManager:
|
PhysicsManager:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
serializedVersion: 11
|
serializedVersion: 13
|
||||||
m_Gravity: {x: 0, y: -9.81, z: 0}
|
m_Gravity: {x: 0, y: -9.81, z: 0}
|
||||||
m_DefaultMaterial: {fileID: 0}
|
m_DefaultMaterial: {fileID: 0}
|
||||||
m_BounceThreshold: 2
|
m_BounceThreshold: 2
|
||||||
m_SleepThreshold: 0.005
|
m_SleepThreshold: 0.005
|
||||||
m_DefaultContactOffset: 0.01
|
m_DefaultContactOffset: 0.01
|
||||||
m_DefaultSolverIterations: 6
|
m_DefaultSolverIterations: 12
|
||||||
m_DefaultSolverVelocityIterations: 1
|
m_DefaultSolverVelocityIterations: 1
|
||||||
m_QueriesHitBackfaces: 0
|
m_QueriesHitBackfaces: 0
|
||||||
m_QueriesHitTriggers: 1
|
m_QueriesHitTriggers: 1
|
||||||
@@ -22,6 +22,7 @@ PhysicsManager:
|
|||||||
m_AutoSyncTransforms: 0
|
m_AutoSyncTransforms: 0
|
||||||
m_ReuseCollisionCallbacks: 1
|
m_ReuseCollisionCallbacks: 1
|
||||||
m_ClothInterCollisionSettingsToggle: 0
|
m_ClothInterCollisionSettingsToggle: 0
|
||||||
|
m_ClothGravity: {x: 0, y: -9.81, z: 0}
|
||||||
m_ContactPairsMode: 0
|
m_ContactPairsMode: 0
|
||||||
m_BroadphaseType: 0
|
m_BroadphaseType: 0
|
||||||
m_WorldBounds:
|
m_WorldBounds:
|
||||||
@@ -31,4 +32,4 @@ PhysicsManager:
|
|||||||
m_FrictionType: 0
|
m_FrictionType: 0
|
||||||
m_EnableEnhancedDeterminism: 0
|
m_EnableEnhancedDeterminism: 0
|
||||||
m_EnableUnifiedHeightmaps: 1
|
m_EnableUnifiedHeightmaps: 1
|
||||||
m_DefaultMaxAngluarSpeed: 7
|
m_DefaultMaxAngularSpeed: 7
|
||||||
|
|||||||
Reference in New Issue
Block a user