HeavenStudio/Assets/Scripts/Transform/LookAlongVelocity.cs

23 lines
441 B
C#
Raw Normal View History

2021-12-19 04:10:43 +00:00
using UnityEngine;
public class LookAlongVelocity : MonoBehaviour
{
public float minVelocity = 0.01f;
public new Rigidbody2D rigidbody;
private void Update ()
{
if (rigidbody == null)
return;
if (rigidbody.velocity.magnitude < minVelocity)
return;
var rotation = transform.eulerAngles;
var angle = Vector2.SignedAngle (Vector2.up, rigidbody.velocity);
rotation.z = angle;
transform.eulerAngles = rotation;
}
}