SHADE_Y3/Assets/Scripts/Gameplay/AIBehaviour/AIRework/RotateToVelocity.cs

41 lines
1.0 KiB
C#
Raw Normal View History

2023-03-24 13:26:18 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SHADE;
namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework
{
public class RotateToVelocity : Script
{
public float rotationPerSecond = 5.0f;
protected override void update()
{
RigidBody rigid = GetComponent<RigidBody>();
Transform transform = GetComponent<Transform>();
if(rigid && transform)
{
Vector3 vel = rigid.LinearVelocity;
if(vel.GetSqrMagnitude() > 1.0f)
{
Quaternion currentRotation = transform.LocalRotation;
Quaternion targetRotation = Quaternion.Euler(0.0f, MathF.Atan2(vel.x, vel.z), 0.0f);
transform.LocalRotation = Quaternion.Slerp(currentRotation, targetRotation, rotationPerSecond * (float)Time.FixedDeltaTime);
}
}
}
}
}