Files

67 lines
1.7 KiB
C#
Raw Permalink Normal View History

2026-01-09 17:21:32 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
public class Movement : MonoBehaviour
{
[SerializeField] InputAction thrust;
[SerializeField] InputAction rotate;
[SerializeField] float thrustStrength = 10f;
[SerializeField] float rotationSpeed = 100f;
[SerializeField] float fuelConsumptionRate = 1f;
Rigidbody rb;
AudioSource audioSource;
PlayerStats playerStats;
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
playerStats = GetComponent<PlayerStats>();
}
void OnEnable()
{
thrust.Enable();
rotate.Enable();
}
void FixedUpdate()
{
ProcessThrust();
ProcessRotation();
}
private void ProcessThrust()
{
if (thrust.IsPressed())
{
rb.AddRelativeForce(Vector3.up * thrustStrength * Time.fixedDeltaTime);
playerStats.UseFuel(fuelConsumptionRate);
if (!audioSource.isPlaying)
{
audioSource.Play();
}
}
else
{
audioSource.Stop();
}
}
private void ProcessRotation()
{
float rotationInput = rotate.ReadValue<float>();
if(rotationInput < 0)
{
ApplyRotation(rotationSpeed);
}
else if(rotationInput > 0)
{
ApplyRotation(-rotationSpeed);
}
}
private void ApplyRotation(float rotationThisFrame)
{
rb.freezeRotation = true; // freezing rotation so we can manually rotate
transform.Rotate(Vector3.forward * rotationThisFrame * Time.fixedDeltaTime);
rb.freezeRotation = false; // unfreezing rotation so the physics system can take over
}
}