2021-12-19 04:10:43 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class ScaleByVelocity : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public enum Axis { X, Y }
|
|
|
|
|
|
|
|
|
|
public float bias = 1f;
|
|
|
|
|
public float strength = 1f;
|
|
|
|
|
public Axis axis = Axis.Y;
|
|
|
|
|
public float size;
|
|
|
|
|
|
|
|
|
|
public new Rigidbody2D rigidbody;
|
|
|
|
|
|
|
|
|
|
private Vector2 startScale;
|
|
|
|
|
|
2023-10-29 19:44:47 +00:00
|
|
|
|
private void Start()
|
2021-12-19 04:10:43 +00:00
|
|
|
|
{
|
|
|
|
|
startScale = transform.localScale;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 19:44:47 +00:00
|
|
|
|
private void Update()
|
2021-12-19 04:10:43 +00:00
|
|
|
|
{
|
|
|
|
|
var velocity = rigidbody.velocity.magnitude;
|
|
|
|
|
|
|
|
|
|
/*if (Mathf.Approximately (velocity, 0f))
|
|
|
|
|
return;*/
|
|
|
|
|
|
|
|
|
|
var amount = velocity * strength + bias;
|
|
|
|
|
var inverseAmount = 1.0f;
|
|
|
|
|
if (velocity > 0.4f)
|
2023-10-29 19:44:47 +00:00
|
|
|
|
inverseAmount = (1f / amount) * startScale.magnitude;
|
2021-12-19 04:10:43 +00:00
|
|
|
|
|
|
|
|
|
switch (axis)
|
|
|
|
|
{
|
|
|
|
|
case Axis.X:
|
2023-10-29 19:44:47 +00:00
|
|
|
|
transform.localScale = new Vector3(amount - 0.414214f, inverseAmount, 1f);
|
2021-12-19 04:10:43 +00:00
|
|
|
|
return;
|
|
|
|
|
case Axis.Y:
|
2023-10-29 19:44:47 +00:00
|
|
|
|
transform.localScale = new Vector3(Mathf.Clamp(inverseAmount, 0.6f, 1f), amount, 1f);
|
2021-12-19 04:10:43 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|