Guest User

PYROS 2137

a guest
Oct 13th, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Kiszak : MonoBehaviour
  4. {
  5. [Header("Ustawienia ruchu")]
  6. public float moveSpeed = 5f;
  7. public float rotateSpeed = 10f;
  8.  
  9. [Header("Ustawienia strzału")]
  10. public GameObject bulletPrefab; // prefab pocisku
  11. public Transform shootPoint; // punkt, z którego Kiszak strzela
  12.  
  13. private bool isMoving = false;
  14. private bool isShooting = false;
  15.  
  16. void Update()
  17. {
  18. HandleMovement();
  19. HandleShooting();
  20. }
  21.  
  22. // --- RUCH WSAD ---
  23. void HandleMovement()
  24. {
  25. if (isShooting) return; // jeśli strzela — nie może się ruszać
  26.  
  27. Vector3 moveDirection = Vector3.zero;
  28.  
  29. if (Input.GetKey(KeyCode.W)) moveDirection += Vector3.forward; // przód
  30. if (Input.GetKey(KeyCode.S)) moveDirection += Vector3.back; // tył
  31. if (Input.GetKey(KeyCode.A)) moveDirection += Vector3.left; // lewo
  32. if (Input.GetKey(KeyCode.D)) moveDirection += Vector3.right; // prawo
  33.  
  34. if (moveDirection != Vector3.zero)
  35. {
  36. isMoving = true;
  37. Move(moveDirection);
  38. }
  39. else
  40. {
  41. StopMoving();
  42. }
  43. }
  44.  
  45. void Move(Vector3 direction)
  46. {
  47. // Obrót w kierunku ruchu
  48. Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
  49. transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
  50.  
  51. // Ruch w tym kierunku
  52. transform.Translate(direction.normalized * moveSpeed * Time.deltaTime, Space.World);
  53. }
  54.  
  55. void StopMoving()
  56. {
  57. if (isMoving)
  58. {
  59. isMoving = false;
  60. // Debug.Log("Kiszak przestał się ruszać.");
  61. }
  62. }
  63.  
  64. // --- STRZAŁ ---
  65. void HandleShooting()
  66. {
  67. // Strzelanie tylko jeśli nie rusza się
  68. if (Input.GetMouseButtonDown(0) && !isMoving)
  69. {
  70. Shoot();
  71. }
  72.  
  73. // Zatrzymanie strzelania po puszczeniu LPM
  74. if (isShooting && !Input.GetMouseButton(0))
  75. {
  76. StopShooting();
  77. }
  78.  
  79. // Podczas strzelania obracaj w stronę kursora
  80. if (isShooting)
  81. {
  82. RotateTowardsMouse();
  83. }
  84. }
  85.  
  86. void Shoot()
  87. {
  88. isShooting = true;
  89. Debug.Log("💥 Kiszak strzela!");
  90.  
  91. if (bulletPrefab != null && shootPoint != null)
  92. {
  93. Instantiate(bulletPrefab, shootPoint.position, shootPoint.rotation);
  94. }
  95. }
  96.  
  97. void StopShooting()
  98. {
  99. isShooting = false;
  100. Debug.Log("Kiszak przestał strzelać.");
  101. }
  102.  
  103. void RotateTowardsMouse()
  104. {
  105. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  106. Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
  107. float distance;
  108.  
  109. if (groundPlane.Raycast(ray, out distance))
  110. {
  111. Vector3 targetPoint = ray.GetPoint(distance);
  112. Vector3 direction = (targetPoint - transform.position);
  113. direction.y = 0;
  114.  
  115. if (direction != Vector3.zero)
  116. {
  117. Quaternion lookRotation = Quaternion.LookRotation(direction);
  118. transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, rotateSpeed * Time.deltaTime);
  119. }
  120. }
  121. }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment