54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Saltosion.OneWeapon.Effects;
|
|
|
|
namespace Saltosion.OneWeapon.Utils {
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
public class AcceleratedMovement : MonoBehaviour {
|
|
|
|
public Bobbing Bobbing;
|
|
|
|
private Vector2 LastDirection = Vector2.zero;
|
|
public Vector2 Direction = Vector2.zero;
|
|
public float MaxSpeed = 7;
|
|
public float AccelerationSpeed = 70;
|
|
public float DecelerationSpeed = 70;
|
|
|
|
[HideInInspector]
|
|
public float AnimationSpeedModifier;
|
|
[HideInInspector]
|
|
public float AnimationSpeed;
|
|
public Vector2 CurrentVelocity { private set; get; }
|
|
public float SpeedPercentage { private set; get; }
|
|
[HideInInspector]
|
|
private float CurrentSpeed = 0;
|
|
|
|
private Rigidbody2D Body;
|
|
|
|
void Start() {
|
|
Body = GetComponent<Rigidbody2D>();
|
|
}
|
|
|
|
void Update() {
|
|
if (Direction.magnitude > 0) {
|
|
CurrentSpeed += AccelerationSpeed * Time.deltaTime;
|
|
} else {
|
|
CurrentSpeed -= DecelerationSpeed * Time.deltaTime;
|
|
}
|
|
CurrentSpeed *= 1 - (Vector2.Angle(Direction, LastDirection) / 180);
|
|
CurrentSpeed = Mathf.Clamp(CurrentSpeed, 0, MaxSpeed);
|
|
SpeedPercentage = CurrentSpeed / MaxSpeed;
|
|
AnimationSpeed = SpeedPercentage * AnimationSpeedModifier;
|
|
|
|
CurrentVelocity = Direction.normalized * CurrentSpeed;
|
|
|
|
LastDirection = Direction;
|
|
Body.velocity = CurrentVelocity;
|
|
if (Bobbing != null) {
|
|
Bobbing.BobbingMultiplier = SpeedPercentage;
|
|
}
|
|
}
|
|
}
|
|
}
|