2019-08-07 22:54:18 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Saltosion.OneWeapon.Effects;
|
|
|
|
|
|
|
|
|
|
namespace Saltosion.OneWeapon.Utils {
|
2019-08-18 19:51:03 +02:00
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
2019-08-07 22:54:18 +02:00
|
|
|
|
public class AcceleratedMovement : MonoBehaviour {
|
|
|
|
|
|
|
|
|
|
public Bobbing Bobbing;
|
|
|
|
|
|
|
|
|
|
private Vector2 LastDirection = Vector2.zero;
|
|
|
|
|
public Vector2 Direction = Vector2.zero;
|
2019-08-14 16:48:17 +02:00
|
|
|
|
public float MaxSpeed = 7;
|
|
|
|
|
public float AccelerationSpeed = 70;
|
2019-08-14 21:31:58 +02:00
|
|
|
|
public float DecelerationSpeed = 70;
|
2019-08-07 22:54:18 +02:00
|
|
|
|
|
2019-08-22 20:36:22 +02:00
|
|
|
|
[HideInInspector]
|
|
|
|
|
public float AnimationSpeedModifier;
|
|
|
|
|
[HideInInspector]
|
|
|
|
|
public float AnimationSpeed;
|
2019-08-07 22:54:18 +02:00
|
|
|
|
public Vector2 CurrentVelocity { private set; get; }
|
|
|
|
|
public float SpeedPercentage { private set; get; }
|
2019-08-22 20:36:22 +02:00
|
|
|
|
[HideInInspector]
|
2019-08-07 22:54:18 +02:00
|
|
|
|
private float CurrentSpeed = 0;
|
|
|
|
|
|
2019-08-18 19:51:03 +02:00
|
|
|
|
private Rigidbody2D Body;
|
|
|
|
|
|
|
|
|
|
void Start() {
|
|
|
|
|
Body = GetComponent<Rigidbody2D>();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-07 22:54:18 +02:00
|
|
|
|
void Update() {
|
|
|
|
|
if (Direction.magnitude > 0) {
|
|
|
|
|
CurrentSpeed += AccelerationSpeed * Time.deltaTime;
|
|
|
|
|
} else {
|
2019-08-14 21:31:58 +02:00
|
|
|
|
CurrentSpeed -= DecelerationSpeed * Time.deltaTime;
|
2019-08-07 22:54:18 +02:00
|
|
|
|
}
|
|
|
|
|
CurrentSpeed *= 1 - (Vector2.Angle(Direction, LastDirection) / 180);
|
|
|
|
|
CurrentSpeed = Mathf.Clamp(CurrentSpeed, 0, MaxSpeed);
|
|
|
|
|
SpeedPercentage = CurrentSpeed / MaxSpeed;
|
2019-08-22 20:36:22 +02:00
|
|
|
|
AnimationSpeed = SpeedPercentage * AnimationSpeedModifier;
|
2019-08-07 22:54:18 +02:00
|
|
|
|
|
|
|
|
|
CurrentVelocity = Direction.normalized * CurrentSpeed;
|
|
|
|
|
|
|
|
|
|
LastDirection = Direction;
|
|
|
|
|
Body.velocity = CurrentVelocity;
|
2019-08-18 19:51:03 +02:00
|
|
|
|
if (Bobbing != null) {
|
|
|
|
|
Bobbing.BobbingMultiplier = SpeedPercentage;
|
|
|
|
|
}
|
2019-08-07 22:54:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-14 16:17:48 +02:00
|
|
|
|
}
|