rj-action-library-godot-dev.../Scripts/GameJam/Player/RotateCharacter.cs

62 lines
1.2 KiB
C#

using Godot;
using Rokojori;
namespace GameJam
{
[GlobalClass]
public partial class RotateCharacter:Node
{
[Export]
public Node3D cameraSource;
[Export]
public Node3D player;
[Export]
public RJSensor up;
[Export]
public RJSensor down;
[Export]
public RJSensor left;
[Export]
public RJSensor right;
[Export]
public float rotationSpeed;
public override void _Process( double delta )
{
var y = up.GetValue() - down.GetValue();
var x = right.GetValue() - left.GetValue();
var v = new Vector2( x, y );
if ( v.Length() < 0.4f )
{
return;
}
var directionZ = y * cameraSource.GlobalBasis.Z;
var directionX = -x * cameraSource.GlobalBasis.X;
var normalized = ( directionX + directionZ ).Normalized();
normalized.Y = 0;
var rotationBefore = player.GlobalBasis.GetRotationQuaternion();
player.LookAt( player.GlobalPosition + normalized * 100f );
var rotationAfter = player.GlobalBasis.GetRotationQuaternion();
var lerped = rotationBefore.Slerp( rotationAfter, rotationSpeed );
Math3D.SetGlobalRotation( player, lerped );
}
}
}