unity-atoms/Examples/Assets/InfiniteWaves/Common/Rigidbody2DExtensions.cs

28 lines
1.0 KiB
C#
Raw Normal View History

2020-03-11 16:11:27 -04:00
using UnityEngine;
2020-03-17 21:04:33 -04:00
namespace UnityAtoms.Examples
2020-03-11 16:11:27 -04:00
{
2020-03-17 21:04:33 -04:00
public static class Rigidbody2DExtensions
2020-03-11 16:11:27 -04:00
{
2020-03-19 20:29:39 -04:00
/// <summary>
/// Simple extension to move a Rigidbody2D using input.
/// </summary>
/// <param name="body">The Rigidbody2D.</param>
/// <param name="input">Move input.</param>
/// <param name="speed">The speed multiplier.</param>
/// <param name="deltaTime">Time since last Update tick.</param>
2020-03-17 21:04:33 -04:00
public static void Move(this Rigidbody2D body, Vector2 input, float speed, float deltaTime)
2020-03-11 16:11:27 -04:00
{
2020-03-17 21:04:33 -04:00
var direction = input.normalized;
var targetVelocity = direction * speed;
body.velocity = Vector2.Lerp(body.velocity, targetVelocity, 10f * deltaTime);
if (direction.magnitude > 0f)
{
float lookAtTargetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
body.transform.rotation = Quaternion.AngleAxis(lookAtTargetAngle, Vector3.forward);
}
2020-03-11 16:11:27 -04:00
}
}
}