1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/PlayerMovement.cs

211 lines
6.8 KiB
C#
Raw Normal View History

using UnityEngine;
public class PlayerMovement : MonoBehaviour {
bool firstKeyPressed;
bool messagePosted;
[SerializeField]
public float acceleration = 3;
2018-04-25 00:32:40 +02:00
[SerializeField]
public float boost = 2;
[SerializeField]
public float friction = 0.1f;
[SerializeField]
public float turnSpeed = 2;
[SerializeField]
public float drift = 1f;
[SerializeField]
public float brake = 2f;
2018-04-25 00:32:40 +02:00
[SerializeField]
public float maxBrakeTime = 5f;
2018-04-23 20:37:21 +02:00
// The time of the acceleration/deceleration sounds in seconds
2018-04-23 23:14:41 +02:00
[SerializeField]
public float accelerationTime = 5;
[SerializeField]
public float decelerationTime = 5;
2018-04-23 20:37:21 +02:00
2018-04-25 00:32:40 +02:00
[SerializeField]
public GameObject leftFrontWheel;
[SerializeField]
public GameObject rightFrontWheel;
2018-04-23 23:14:41 +02:00
float brakeTime;
float lastFrame;
2018-04-23 21:30:31 +02:00
2018-04-23 20:37:21 +02:00
public enum SpeedState
{
SLOW, FASTER, FAST, SLOWER
}
2018-04-23 23:14:41 +02:00
SpeedState state;
double changeTime;
2018-04-23 20:37:21 +02:00
Rigidbody2D rb;
// Use this for initialization
void Start() {
rb = GetComponent<Rigidbody2D>();
messagePosted = false;
2018-04-23 20:37:21 +02:00
state = SpeedState.SLOW;
2018-04-23 21:30:31 +02:00
brakeTime = 0;
lastFrame = Time.time;
2018-04-23 23:14:41 +02:00
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.slowdriving);
}
void Update() {
if (!firstKeyPressed && !messagePosted) {
messagePosted = true;
GameController.instance.GetUI().GetNotificationManager().ShowMessage("Press any key to start!", 2);
}
if ( !firstKeyPressed && Input.anyKey ) {
firstKeyPressed = true;
lastFrame = Time.time;
2018-04-25 00:32:40 +02:00
if (Input.GetAxis("Vertical") >= 0) {
state = SpeedState.FASTER;
2018-04-23 23:14:41 +02:00
changeTime = Time.time;
GameController.instance.GetAudioControl().SfxStop(AudioControl.Sfx.slowdriving);
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.faster);
}
}
}
void FixedUpdate() {
if ( !firstKeyPressed )
return;
Vector3 speedVec = new Vector3(rb.velocity.x, rb.velocity.y, 0);
float speed = speedVec.magnitude;
2018-04-25 00:32:40 +02:00
2018-04-23 20:37:21 +02:00
bool braking = Input.GetAxis("Vertical") < 0;
2018-04-23 23:30:32 +02:00
if (braking && brakeTime >= maxBrakeTime) {
2018-04-23 21:30:31 +02:00
brakeTime = maxBrakeTime;
braking = false;
2018-04-23 23:30:32 +02:00
} else if (!braking) {
2018-04-24 00:38:31 +02:00
//brakeTime -= (Time.time - lastFrame) * 0.1f;
2018-04-23 21:30:31 +02:00
}
2018-04-23 20:37:21 +02:00
if (braking) {
2018-04-23 21:30:31 +02:00
brakeTime += Time.time - lastFrame;
2018-04-23 23:14:41 +02:00
GameController.instance.GetAudioControl().SfxStop(AudioControl.Sfx.driving);
2018-04-23 20:37:21 +02:00
switch (state) {
case SpeedState.FASTER:
if (Time.time - changeTime > accelerationTime)
2018-04-23 23:14:41 +02:00
{
changeTime = Time.time;
state = SpeedState.SLOWER;
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.slower);
}
2018-04-23 20:37:21 +02:00
break;
case SpeedState.FAST:
changeTime = Time.time;
state = SpeedState.SLOWER;
2018-04-23 23:14:41 +02:00
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.slower);
2018-04-23 20:37:21 +02:00
break;
case SpeedState.SLOWER:
if (Time.time - changeTime > decelerationTime)
2018-04-23 23:14:41 +02:00
{
2018-04-23 20:37:21 +02:00
state = SpeedState.SLOW;
2018-04-23 23:14:41 +02:00
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.slowdriving);
}
2018-04-23 20:37:21 +02:00
break;
case SpeedState.SLOW:
break;
}
} else {
2018-04-23 23:30:32 +02:00
if (brakeTime < 0)
brakeTime = 0;
2018-04-23 23:14:41 +02:00
GameController.instance.GetAudioControl().SfxStop(AudioControl.Sfx.slowdriving);
2018-04-23 20:37:21 +02:00
switch (state)
{
case SpeedState.FASTER:
2018-04-23 23:14:41 +02:00
if (Time.time - changeTime > accelerationTime) {
state = SpeedState.FAST;
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.driving);
}
2018-04-23 20:37:21 +02:00
break;
case SpeedState.FAST:
break;
case SpeedState.SLOWER:
2018-04-23 23:14:41 +02:00
if (Time.time - changeTime > decelerationTime) {
changeTime = Time.time;
state = SpeedState.FASTER;
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.faster);
}
2018-04-23 20:37:21 +02:00
break;
case SpeedState.SLOW:
changeTime = Time.time;
2018-04-23 23:14:41 +02:00
state = SpeedState.FASTER;
GameController.instance.GetAudioControl().SfxPlay(AudioControl.Sfx.faster);
2018-04-23 20:37:21 +02:00
break;
}
}
2018-04-22 16:32:42 +02:00
{ // Forward
Vector3 acc = transform.up * acceleration;
2018-04-23 20:37:21 +02:00
if (braking)
2018-04-22 16:32:42 +02:00
acc *= 0;
2018-04-24 16:06:43 +02:00
if (Input.GetAxis("Vertical") > 0)
acc *= boost;
2018-04-22 16:32:42 +02:00
rb.AddForce(acc);
2018-04-21 17:22:29 +02:00
}
2018-04-22 16:32:42 +02:00
{// Drag
Vector3 drag = speedVec.normalized * speed * speed * friction * -1;
2018-04-23 20:37:21 +02:00
if (braking) {
2018-04-22 16:32:42 +02:00
drag *= brake;
drag *= speed;
}
rb.AddForce(drag);
Debug.DrawLine(transform.position, transform.position + drag, Color.cyan, 0.01f, false);
2018-04-21 17:22:29 +02:00
}
{ // Drift
Vector3 forwardNorm = ( transform.localRotation * Vector3.up ).normalized * speedVec.magnitude;
Vector3 br = forwardNorm - speedVec;
br *= drift;
rb.AddForce(br);
//Debug.Log(br);
Debug.DrawLine(transform.position, transform.position + br, Color.red, 0.01f, false);
}
2018-04-23 18:21:09 +02:00
//transform.Rotate(Vector3.forward * turnSpeed);
if (Input.GetAxis("Horizontal") < 0)
{
//rb.rotation += turnSpeed;
2018-04-25 00:32:40 +02:00
leftFrontWheel.GetComponent<Animator>().SetBool("left", true);
leftFrontWheel.GetComponent<Animator>().SetBool("right", false);
rightFrontWheel.GetComponent<Animator>().SetBool("left", true);
rightFrontWheel.GetComponent<Animator>().SetBool("right", false);
rb.MoveRotation(rb.rotation + turnSpeed);
2018-04-23 18:21:09 +02:00
} else
2018-04-21 22:08:39 +02:00
//transform.Rotate(Vector3.forward * turnSpeed);
2018-04-23 18:21:09 +02:00
if (Input.GetAxis("Horizontal") > 0)
{
2018-04-25 00:32:40 +02:00
leftFrontWheel.GetComponent<Animator>().SetBool("left", false);
leftFrontWheel.GetComponent<Animator>().SetBool("right", true);
rightFrontWheel.GetComponent<Animator>().SetBool("left", false);
rightFrontWheel.GetComponent<Animator>().SetBool("right", true);
2018-04-23 18:21:09 +02:00
//rb.rotation -= turnSpeed;
rb.MoveRotation(rb.rotation - turnSpeed);
} else {
2018-04-25 00:32:40 +02:00
leftFrontWheel.GetComponent<Animator>().SetBool("left", false);
leftFrontWheel.GetComponent<Animator>().SetBool("right", false);
rightFrontWheel.GetComponent<Animator>().SetBool("left", false);
rightFrontWheel.GetComponent<Animator>().SetBool("right", false);
2018-04-23 18:21:09 +02:00
rb.MoveRotation(rb.rotation);
}
//transform.Rotate(Vector3.forward * -turnSpeed);
// Debug lines
Debug.DrawLine(transform.position, transform.position + speedVec, Color.magenta, 0.01f, false);
Debug.DrawLine(transform.position, transform.position + transform.localRotation * Vector3.up, Color.yellow, 0.01f, false);
2018-04-23 21:30:31 +02:00
//Debug.Log(transform.localRotation.eulerAngles);
//Debug.Log(transform.localRotation * Vector3.up);
//Debug.Log(curspeed);
lastFrame = Time.time;
}
2018-04-23 21:30:31 +02:00
/// <returns>The time in seconds the player was braking</returns>
public float GetBrakeTime() {
return brakeTime;
}
}