bugfix/fix-ship (#49)

Co-authored-by: Djelal BOUDJI <djelal@gmail.com>
This commit is contained in:
djelalb
2025-03-31 09:57:45 +02:00
committed by GitHub
parent d57cc647aa
commit aa6401f6bc
4 changed files with 61 additions and 34 deletions

View File

@ -6,34 +6,51 @@ public class ShipGameMode : IGameMode
private const float HorizontalSpeed = 8.6f;
private const float JumpForce = 26.6581f;
private const KeyCode JumpKey = KeyCode.Space;
private const float UpperAngle = 45f;
private const float LowerAngle = -45f;
private const float RotationLerpSpeed = 5f;
private const float RotationTransitionDuration = 0.5f;
public void Update(Player player)
{
player.RigidBody.linearVelocity = new Vector2(HorizontalSpeed, player.RigidBody.linearVelocity.y);
if (player.HasStarted && Input.GetKey(JumpKey))
bool jumpPressed = Input.GetKey(JumpKey);
if (player.HasStarted && jumpPressed)
{
Jump(player);
if (Input.GetKeyDown(JumpKey))
{
player.Transform.rotation = Quaternion.Euler(0, 0, UpperAngle);
}
else
{
player.Transform.rotation = Quaternion.Euler(0, 0, UpperAngle);
}
}
else
{
float currentAngle = GetCurrentZAngle(player);
float t = Mathf.Clamp01(Time.deltaTime / RotationTransitionDuration);
float interpolationFactor = Mathf.Sin(t * (Mathf.PI / 2));
float newAngle = Mathf.Lerp(currentAngle, LowerAngle, interpolationFactor);
player.Transform.rotation = Quaternion.Euler(0, 0, newAngle);
}
float targetAngle = Input.GetKey(JumpKey) ? UpperAngle : LowerAngle;
float currentAngle = player.Transform.rotation.eulerAngles.z;
if (currentAngle > 180f)
currentAngle -= 360f;
float newAngle = Mathf.Lerp(currentAngle, targetAngle, RotationLerpSpeed * Time.deltaTime);
player.Transform.rotation = Quaternion.Euler(0, 0, newAngle);
UpdateParticlePositionAndRotation(player);
if (player.Particle.gameObject.activeSelf)
{
player.Particle.gameObject.SetActive(false);
}
}
private void UpdateParticlePositionAndRotation(Player player)
private float GetCurrentZAngle(Player player)
{
player.Particle.transform.position = player.Transform.position + new Vector3(-0.19f, -0.64f, -10);
player.Particle.transform.rotation = Quaternion.Euler(0, 0, 150.464f);
float angle = player.Transform.rotation.eulerAngles.z;
if (angle > 180f)
angle -= 360f;
return angle;
}
private void Jump(Player player)
@ -56,7 +73,7 @@ public class ShipGameMode : IGameMode
return;
}
float currentAngle = player.Transform.rotation.eulerAngles.z;
float currentAngle = GetCurrentZAngle(player);
float shortestAngle = Mathf.DeltaAngle(currentAngle, 0);
player.Transform.rotation = Quaternion.RotateTowards(player.Transform.rotation, Quaternion.Euler(0, 0, 0), Mathf.Abs(shortestAngle));
}