Files
BoostBaller/Assets/Scripts/PlayerController.cs
2020-01-27 16:31:38 +01:00

127 lines
3.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public bool aiControlled = false;
public string playerNumber;
public bool useController = false;
public float rotationSpeed = 100;
public float movementSpeed = 100;
public float drag = 1;
public float idleDrag = 2;
public Rigidbody rb;
public GameObject forceZone;
public float boostSpeed = 0;
public float boostDuration = 0;
public float lastBoostTime = 0;
public bool lockBoost = false;
public AudioClip boostSound;
public Vector2 pitchModifier;
private AudioSource audioSource;
private AIController aiController;
public float inputTimeout = 0;
// Start is called before the first frame update
void Start()
{
this.audioSource = this.forceZone.transform.GetComponent<AudioSource>();
this.rb = GetComponent<Rigidbody>();
if (this.aiControlled)
{
this.aiController = GetComponent<AIController>();
}
}
// Update is called once per frame
void Update()
{
float rotationDirection = Input.GetAxis("Horizontal" + this.playerNumber);
float movementDirection = Input.GetAxis("Vertical" + this.playerNumber);
float[] aiInput = new float[3];
this.rb.drag = this.idleDrag;
if(this.inputTimeout > 0) {
this.inputTimeout -= Time.deltaTime;
return;
}
//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) {
//Debug.Log(Input.GetAxis("VerticalBack" + this.playerNumber));
movementDirection = Input.GetAxis("Horizontal" + this.playerNumber) != 0 ? 1 : movementDirection;
float horizontal = Input.GetAxis("Horizontal3");
float vertical = Input.GetAxis("Vertical3");
if (movementDirection != 0)
{
Vector3 direction = new Vector3(horizontal, 0, -vertical).normalized;
this.transform.rotation = Quaternion.LookRotation(direction, Vector3.up);
}
}
//Links
if (rotationDirection > 0)
{
transform.Rotate(Vector3.up, this.rotationSpeed * Time.deltaTime);
}
//Rechts
if (rotationDirection < 0)
{
transform.Rotate(Vector3.up, -this.rotationSpeed * Time.deltaTime);
}
Debug.Log(movementDirection);
//Forward
if (movementDirection != 0)
{
this.rb.drag = this.drag;
this.rb.AddRelativeForce(Vector3.forward * this.movementSpeed * Time.deltaTime, ForceMode.Impulse);
}
//Zurück
//if (movementDirection < 0)
//{
// this.rb.AddRelativeForce(Vector3.back * this.movementSpeed / 2 * Time.deltaTime, ForceMode.Impulse);
//}
//Boost
if (Input.GetButtonDown("Boost" + this.playerNumber) || aiInput[AiInputs.BOOST] > 0 && !this.lockBoost)
{
this.audioSource.clip = this.boostSound;
this.audioSource.pitch = Random.Range(this.pitchModifier.x, this.pitchModifier.y);
this.rb.AddRelativeForce(Vector3.forward * this.boostSpeed * Time.deltaTime, ForceMode.Impulse);
this.forceZone.SetActive(true);
this.audioSource.Play();
this.lastBoostTime = Time.time;
this.lockBoost = true;
}
//Yeet
if (Time.time > (this.lastBoostTime + this.boostDuration))
{
this.lockBoost = false;
this.forceZone.SetActive(false);
}
}
public void InputTimeout(float t)
{
this.inputTimeout = t;
}
}