Guest User

Player source code

a guest
Apr 25th, 2026
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using TMPro;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7. // Rigidbody of the player.
  8. private Rigidbody rb;
  9.  
  10. // Variable to keep track of collected "PickUp" objects.
  11. private int count;
  12.  
  13. // Movement along X and Y axes.
  14. private float movementX;
  15. private float movementY;
  16.  
  17. // Speed at which the player moves.
  18. public float speed = 0;
  19.  
  20. // UI text component to display count of "PickUp" objects collected.
  21. public TextMeshProUGUI countText;
  22.  
  23. // UI object to display winning text.
  24. public GameObject winTextObject;
  25.  
  26. // Start is called before the first frame update.
  27. void Start()
  28. {
  29. // Get and store the Rigidbody component attached to the player.
  30. rb = GetComponent<Rigidbody>();
  31.  
  32. // Initialize count to zero.
  33. count = 0;
  34.  
  35. // Update the count display.
  36. SetCountText();
  37.  
  38. // Initially set the win text to be inactive.
  39. winTextObject.SetActive(false);
  40. }
  41.  
  42. // This function is called when a move input is detected.
  43. void OnMove(InputValue movementValue)
  44. {
  45. // Convert the input value into a Vector2 for movement.
  46. Vector2 movementVector = movementValue.Get<Vector2>();
  47.  
  48. // Store the X and Y components of the movement.
  49. movementX = movementVector.x;
  50. movementY = movementVector.y;
  51. }
  52.  
  53. // FixedUpdate is called once per fixed frame-rate frame.
  54. void FixedUpdate()
  55. {
  56. // Create a 3D movement vector using the X and Y inputs.
  57. Vector3 movement = new Vector3 (movementX, 0.0f, movementY);
  58.  
  59. // Apply force to the Rigidbody to move the player.
  60. rb.AddForce(movement * speed);
  61. }
  62.  
  63.  
  64. void OnTriggerEnter(Collider other)
  65. {
  66. // Check if the object the player collided with has the "PickUp" tag.
  67. if (other.gameObject.CompareTag("PickUp"))
  68. {
  69. // Deactivate the collided object (making it disappear).
  70. other.gameObject.SetActive(false);
  71.  
  72. // Increment the count of "PickUp" objects collected.
  73. count = count + 1;
  74.  
  75. // Update the count display.
  76. SetCountText();
  77. }
  78. }
  79.  
  80. // Function to update the displayed count of "PickUp" objects collected.
  81. void SetCountText()
  82. {
  83. // Update the count text with the current count.
  84. countText.text = "Count: " + count.ToString();
  85.  
  86. // Check if the count has reached or exceeded the win condition.
  87. if (count >= 12)
  88. {
  89. // Display the win text.
  90. winTextObject.SetActive(true);
  91.  
  92. // Destroy the enemy GameObject.
  93. Destroy(GameObject.FindGameObjectWithTag("Enemy"));
  94. }
  95. }
  96.  
  97. private void OnCollisionEnter(Collision collision)
  98. {
  99. if (collision.gameObject.CompareTag("Enemy"))
  100. {
  101. // Destroy the current object
  102. Destroy(gameObject);
  103.  
  104. // Update the winText to display "You Lose!"
  105. winTextObject.gameObject.SetActive(true);
  106. winTextObject.GetComponent<TextMeshProUGUI>().text = "You Lose!";
  107.  
  108. }
  109.  
  110. }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment