Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Kiszak : MonoBehaviour
- {
- [Header("Ustawienia ruchu")]
- public float moveSpeed = 5f;
- public float rotateSpeed = 10f;
- [Header("Ustawienia strzału")]
- public GameObject bulletPrefab; // prefab pocisku
- public Transform shootPoint; // punkt, z którego Kiszak strzela
- private bool isMoving = false;
- private bool isShooting = false;
- void Update()
- {
- HandleMovement();
- HandleShooting();
- }
- // --- RUCH WSAD ---
- void HandleMovement()
- {
- if (isShooting) return; // jeśli strzela — nie może się ruszać
- Vector3 moveDirection = Vector3.zero;
- if (Input.GetKey(KeyCode.W)) moveDirection += Vector3.forward; // przód
- if (Input.GetKey(KeyCode.S)) moveDirection += Vector3.back; // tył
- if (Input.GetKey(KeyCode.A)) moveDirection += Vector3.left; // lewo
- if (Input.GetKey(KeyCode.D)) moveDirection += Vector3.right; // prawo
- if (moveDirection != Vector3.zero)
- {
- isMoving = true;
- Move(moveDirection);
- }
- else
- {
- StopMoving();
- }
- }
- void Move(Vector3 direction)
- {
- // Obrót w kierunku ruchu
- Quaternion targetRotation = Quaternion.LookRotation(direction, Vector3.up);
- transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
- // Ruch w tym kierunku
- transform.Translate(direction.normalized * moveSpeed * Time.deltaTime, Space.World);
- }
- void StopMoving()
- {
- if (isMoving)
- {
- isMoving = false;
- // Debug.Log("Kiszak przestał się ruszać.");
- }
- }
- // --- STRZAŁ ---
- void HandleShooting()
- {
- // Strzelanie tylko jeśli nie rusza się
- if (Input.GetMouseButtonDown(0) && !isMoving)
- {
- Shoot();
- }
- // Zatrzymanie strzelania po puszczeniu LPM
- if (isShooting && !Input.GetMouseButton(0))
- {
- StopShooting();
- }
- // Podczas strzelania obracaj w stronę kursora
- if (isShooting)
- {
- RotateTowardsMouse();
- }
- }
- void Shoot()
- {
- isShooting = true;
- Debug.Log("💥 Kiszak strzela!");
- if (bulletPrefab != null && shootPoint != null)
- {
- Instantiate(bulletPrefab, shootPoint.position, shootPoint.rotation);
- }
- }
- void StopShooting()
- {
- isShooting = false;
- Debug.Log("Kiszak przestał strzelać.");
- }
- void RotateTowardsMouse()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
- float distance;
- if (groundPlane.Raycast(ray, out distance))
- {
- Vector3 targetPoint = ray.GetPoint(distance);
- Vector3 direction = (targetPoint - transform.position);
- direction.y = 0;
- if (direction != Vector3.zero)
- {
- Quaternion lookRotation = Quaternion.LookRotation(direction);
- transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, rotateSpeed * Time.deltaTime);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment