BloodAndGore/Assets/Scripts/Utils/AcceleratedMovement.cs

39 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Saltosion.OneWeapon.Effects;
namespace Saltosion.OneWeapon.Utils {
public class AcceleratedMovement : MonoBehaviour {
public Rigidbody2D Body;
public Bobbing Bobbing;
private Vector2 LastDirection = Vector2.zero;
public Vector2 Direction = Vector2.zero;
public float MaxSpeed = 7;
public float AccelerationSpeed = 70;
public Vector2 CurrentVelocity { private set; get; }
public float SpeedPercentage { private set; get; }
private float CurrentSpeed = 0;
void Update() {
if (Direction.magnitude > 0) {
CurrentSpeed += AccelerationSpeed * Time.deltaTime;
} else {
CurrentSpeed -= AccelerationSpeed * Time.deltaTime;
}
CurrentSpeed *= 1 - (Vector2.Angle(Direction, LastDirection) / 180);
CurrentSpeed = Mathf.Clamp(CurrentSpeed, 0, MaxSpeed);
SpeedPercentage = CurrentSpeed / MaxSpeed;
CurrentVelocity = Direction.normalized * CurrentSpeed;
LastDirection = Direction;
Body.velocity = CurrentVelocity;
Bobbing.BobbingMultiplier = SpeedPercentage;
}
}
}