Advertisement
Guest User

Untitled

a guest
Aug 1st, 2021
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7. public float thrustSpeed = 1.0f;
  8. public float turnSpeed = 1.0f;
  9.  
  10. private Rigidbody2D _rigidbody;
  11.  
  12. private bool _thrusting;
  13.  
  14. private float _turnDirection;
  15.  
  16. private void Awake()
  17. {
  18. _rigidbody = GetComponent<Rigidbody2D>();
  19. }
  20.  
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24.  
  25. }
  26.  
  27. // Update is called once per frame
  28. private void Update()
  29. {
  30. _thrusting = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
  31.  
  32. if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
  33. {
  34. _turnDirection = 1.0f;
  35. }
  36. else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
  37. {
  38. _turnDirection = -1.0f;
  39. }
  40. else
  41. {
  42. _turnDirection = 0.0f;
  43. }
  44. }
  45.  
  46. private void FixedUpdate()
  47. {
  48. if (_thrusting)
  49. {
  50. _rigidbody.AddForce(this.transform.up * this.thrustSpeed);
  51. }
  52. if (_turnDirection > 0.0f) {
  53. _rigidbody.AddTorque(_turnDirection * this.turnSpeed);
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement