270 lines
6.0 KiB
C#
270 lines
6.0 KiB
C#
|
|
using UnityEngine;
|
|
|
|
public class CharacterRotation : MonoBehaviour
|
|
{
|
|
[Header("Visual")]
|
|
[SerializeField]
|
|
private Transform visual;
|
|
|
|
[Header("Rotation")]
|
|
[SerializeField]
|
|
private float rotationSpeed = 540f;
|
|
|
|
[Tooltip("In welche lokale Richtung zeigt die Grafik standardmäßig?")]
|
|
[SerializeField]
|
|
private SpriteForwardDirection spriteForward =
|
|
SpriteForwardDirection.Up;
|
|
|
|
private IMovementDirection movement;
|
|
|
|
private enum SpriteForwardDirection
|
|
{
|
|
Up,
|
|
Right
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
movement =
|
|
GetComponent<IMovementDirection>();
|
|
|
|
if (movement == null)
|
|
{
|
|
Debug.LogError(
|
|
"CharacterRotation benötigt eine Komponente, " +
|
|
"die IMovementDirection implementiert.",
|
|
this
|
|
);
|
|
}
|
|
|
|
if (visual == null)
|
|
{
|
|
Debug.LogError(
|
|
"CharacterRotation: Kein Visual zugewiesen.",
|
|
this
|
|
);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (movement == null || visual == null)
|
|
return;
|
|
|
|
Vector2 direction =
|
|
movement.MovementDirection;
|
|
|
|
// Keine Bewegung -> aktuelle Rotation behalten.
|
|
if (direction.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
RotateTowards(direction);
|
|
}
|
|
|
|
|
|
// ROTATIONSLOGIK
|
|
//
|
|
|
|
public void SetInitialRotation()
|
|
{
|
|
if (movement == null || visual == null)
|
|
return;
|
|
|
|
Vector2 direction =
|
|
movement.MovementDirection;
|
|
|
|
if (direction.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
visual.rotation =
|
|
GetTargetRotation(direction);
|
|
}
|
|
|
|
private void RotateTowards(Vector2 direction)
|
|
{
|
|
Quaternion targetRotation =
|
|
GetTargetRotation(direction);
|
|
|
|
visual.rotation =
|
|
Quaternion.RotateTowards(
|
|
visual.rotation,
|
|
targetRotation,
|
|
rotationSpeed * Time.deltaTime
|
|
);
|
|
}
|
|
|
|
private Quaternion GetTargetRotation(Vector2 direction)
|
|
{
|
|
float targetAngle =
|
|
Mathf.Atan2(
|
|
direction.y,
|
|
direction.x
|
|
) * Mathf.Rad2Deg;
|
|
|
|
if (spriteForward ==
|
|
SpriteForwardDirection.Up)
|
|
{
|
|
targetAngle -= 90f;
|
|
}
|
|
|
|
return Quaternion.Euler(
|
|
0f,
|
|
0f,
|
|
targetAngle
|
|
);
|
|
}
|
|
|
|
/*
|
|
[Header("Visual")]
|
|
[SerializeField]
|
|
private Transform visual;
|
|
|
|
[Header("Rotation")]
|
|
[SerializeField]
|
|
private float maxRotationSpeed = 720f;
|
|
|
|
[SerializeField]
|
|
private float rotationAcceleration = 3f;
|
|
|
|
[Tooltip("Ab welcher Winkeländerung eine neue Drehung erkannt wird.")]
|
|
[SerializeField]
|
|
private float directionChangeThreshold = 5f;
|
|
|
|
[Tooltip("In welche lokale Richtung zeigt die Grafik standardmäßig?")]
|
|
[SerializeField]
|
|
private SpriteForwardDirection spriteForward =
|
|
SpriteForwardDirection.Up;
|
|
|
|
private IMovementDirection movement;
|
|
|
|
private float rotationTime = 0f;
|
|
|
|
private Quaternion lastTargetRotation;
|
|
private bool hasLastTargetRotation = false;
|
|
|
|
private enum SpriteForwardDirection
|
|
{
|
|
Up,
|
|
Right
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
movement = GetComponent<IMovementDirection>();
|
|
|
|
if (movement == null)
|
|
{
|
|
Debug.LogError(
|
|
"CharacterRotation benötigt eine Komponente, " +
|
|
"die IMovementDirection implementiert.",
|
|
this
|
|
);
|
|
}
|
|
|
|
if (visual == null)
|
|
{
|
|
Debug.LogError(
|
|
"CharacterRotation: Kein Visual zugewiesen.",
|
|
this
|
|
);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (movement == null || visual == null)
|
|
return;
|
|
|
|
Vector2 direction = movement.MovementDirection;
|
|
|
|
// Keine Bewegung -> aktuelle Rotation behalten.
|
|
if (direction.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
RotateTowards(direction);
|
|
}
|
|
|
|
// ROTATIONSLOGIK
|
|
|
|
public void SetInitialRotation()
|
|
{
|
|
if (movement == null || visual == null)
|
|
return;
|
|
|
|
Vector2 direction = movement.MovementDirection;
|
|
|
|
if (direction.sqrMagnitude < 0.001f)
|
|
return;
|
|
|
|
Quaternion targetRotation = GetTargetRotation(direction);
|
|
|
|
visual.rotation = targetRotation;
|
|
|
|
rotationTime = 0f;
|
|
lastTargetRotation = targetRotation;
|
|
hasLastTargetRotation = true;
|
|
}
|
|
|
|
private void RotateTowards(Vector2 direction)
|
|
{
|
|
Quaternion targetRotation = GetTargetRotation(direction);
|
|
|
|
// Prüfen, ob sich die Zielrichtung geändert hat.
|
|
if (!hasLastTargetRotation ||
|
|
Quaternion.Angle(lastTargetRotation, targetRotation)
|
|
> directionChangeThreshold)
|
|
{
|
|
rotationTime = 0f;
|
|
lastTargetRotation = targetRotation;
|
|
hasLastTargetRotation = true;
|
|
}
|
|
|
|
// Drehdauer erhöhen.
|
|
rotationTime += Time.deltaTime;
|
|
|
|
// Exponentielle Beschleunigung.
|
|
float speed =
|
|
maxRotationSpeed *
|
|
(1f - Mathf.Exp(-rotationAcceleration * rotationTime));
|
|
|
|
// Rotation durchführen.
|
|
visual.rotation =
|
|
Quaternion.RotateTowards(
|
|
visual.rotation,
|
|
targetRotation,
|
|
speed * Time.deltaTime
|
|
);
|
|
|
|
// Drehung abgeschlossen?
|
|
if (Quaternion.Angle(
|
|
visual.rotation,
|
|
targetRotation
|
|
) < 0.1f)
|
|
{
|
|
rotationTime = 0f;
|
|
}
|
|
}
|
|
|
|
private Quaternion GetTargetRotation(Vector2 direction)
|
|
{
|
|
float targetAngle =
|
|
Mathf.Atan2(
|
|
direction.y,
|
|
direction.x
|
|
) * Mathf.Rad2Deg;
|
|
|
|
if (spriteForward == SpriteForwardDirection.Up)
|
|
{
|
|
targetAngle -= 90f;
|
|
}
|
|
|
|
return Quaternion.Euler(
|
|
0f,
|
|
0f,
|
|
targetAngle
|
|
);
|
|
}
|
|
*/
|
|
}
|