Player speed state
This commit is contained in:
parent
2b5a0b4d67
commit
1fab4820de
1 changed files with 55 additions and 2 deletions
|
@ -15,12 +15,25 @@ public class PlayerMovement : MonoBehaviour {
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
public float brake = 2f;
|
public float brake = 2f;
|
||||||
|
|
||||||
|
// The time of the acceleration/deceleration sounds in seconds
|
||||||
|
public double accelerationTime;
|
||||||
|
public double decelerationTime;
|
||||||
|
|
||||||
|
public enum SpeedState
|
||||||
|
{
|
||||||
|
SLOW, FASTER, FAST, SLOWER
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpeedState state;
|
||||||
|
public double changeTime;
|
||||||
|
|
||||||
Rigidbody2D rb;
|
Rigidbody2D rb;
|
||||||
|
|
||||||
// Use this for initialization
|
// Use this for initialization
|
||||||
void Start() {
|
void Start() {
|
||||||
rb = GetComponent<Rigidbody2D>();
|
rb = GetComponent<Rigidbody2D>();
|
||||||
messagePosted = false;
|
messagePosted = false;
|
||||||
|
state = SpeedState.SLOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update() {
|
void Update() {
|
||||||
|
@ -30,6 +43,8 @@ public class PlayerMovement : MonoBehaviour {
|
||||||
}
|
}
|
||||||
if ( !firstKeyPressed && Input.anyKey ) {
|
if ( !firstKeyPressed && Input.anyKey ) {
|
||||||
firstKeyPressed = true;
|
firstKeyPressed = true;
|
||||||
|
state = SpeedState.FASTER;
|
||||||
|
changeTime = Time.time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,15 +55,53 @@ public class PlayerMovement : MonoBehaviour {
|
||||||
Vector3 speedVec = new Vector3(rb.velocity.x, rb.velocity.y, 0);
|
Vector3 speedVec = new Vector3(rb.velocity.x, rb.velocity.y, 0);
|
||||||
float speed = speedVec.magnitude;
|
float speed = speedVec.magnitude;
|
||||||
|
|
||||||
|
bool braking = Input.GetAxis("Vertical") < 0;
|
||||||
|
if (braking) {
|
||||||
|
switch (state) {
|
||||||
|
case SpeedState.FASTER:
|
||||||
|
if (Time.time - changeTime > accelerationTime)
|
||||||
|
state = SpeedState.SLOWER;
|
||||||
|
break;
|
||||||
|
case SpeedState.FAST:
|
||||||
|
changeTime = Time.time;
|
||||||
|
state = SpeedState.SLOWER;
|
||||||
|
break;
|
||||||
|
case SpeedState.SLOWER:
|
||||||
|
if (Time.time - changeTime > decelerationTime)
|
||||||
|
state = SpeedState.SLOW;
|
||||||
|
break;
|
||||||
|
case SpeedState.SLOW:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case SpeedState.FASTER:
|
||||||
|
if (Time.time - changeTime > accelerationTime)
|
||||||
|
state = SpeedState.FAST;
|
||||||
|
break;
|
||||||
|
case SpeedState.FAST:
|
||||||
|
break;
|
||||||
|
case SpeedState.SLOWER:
|
||||||
|
if (Time.time - changeTime > decelerationTime)
|
||||||
|
state = SpeedState.FASTER;
|
||||||
|
break;
|
||||||
|
case SpeedState.SLOW:
|
||||||
|
changeTime = Time.time;
|
||||||
|
state = SpeedState.FASTER;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
{ // Forward
|
{ // Forward
|
||||||
Vector3 acc = transform.up * acceleration;
|
Vector3 acc = transform.up * acceleration;
|
||||||
if (Input.GetAxis("Vertical") < 0)
|
if (braking)
|
||||||
acc *= 0;
|
acc *= 0;
|
||||||
rb.AddForce(acc);
|
rb.AddForce(acc);
|
||||||
}
|
}
|
||||||
{// Drag
|
{// Drag
|
||||||
Vector3 drag = speedVec.normalized * speed * speed * friction * -1;
|
Vector3 drag = speedVec.normalized * speed * speed * friction * -1;
|
||||||
if (Input.GetAxis("Vertical") < 0) {
|
if (braking) {
|
||||||
drag *= brake;
|
drag *= brake;
|
||||||
drag *= speed;
|
drag *= speed;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue