Engine.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4. public class Engine : MonoBehaviour
  5. {
  6. public RocketDataScript rocketData;
  7. private InputAction jumpAction;
  8. private InputAction thrustLeverAction;
  9. // Start is called once before the first execution of Update after the MonoBehaviour is created
  10. void Start()
  11. {
  12. jumpAction = InputSystem.actions.FindAction("Jump");
  13. thrustLeverAction = InputSystem.actions.FindAction("ThrustLever");
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. if (jumpAction.IsPressed())
  19. Thrust();
  20. var lever = thrustLeverAction.ReadValue<float>();
  21. if (lever != 0)
  22. {
  23. var newLever = rocketData.ThrustLever + lever * rocketData.thrustLeverStep * Time.deltaTime;
  24. newLever = Mathf.Clamp(newLever, 0, 1);
  25. rocketData.SetThrustLever(newLever);
  26. }
  27. }
  28. private void Thrust()
  29. {
  30. if (rocketData.Fuel > 0)
  31. {
  32. rocketData.SetThrust(rocketData.Force);
  33. var rb = GetComponent<Rigidbody>();
  34. rb.AddForce(rocketData.Force * rocketData.ThrustLever * Time.deltaTime * transform.up);
  35. rocketData.AddFuel(-1 * rocketData.ThrustLever * Time.deltaTime);
  36. }
  37. }
  38. }