player.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class player : MonoBehaviour
  6. {
  7. [HideInInspector]
  8. public Animator anim;
  9. [Header("Скорость танка")]
  10. public float speed;
  11. [HideInInspector]
  12. public Rigidbody2D rb;
  13. [HideInInspector]
  14. public float moveInput;
  15. [HideInInspector]
  16. public bool faceRight;
  17. [HideInInspector]
  18. public GameObject Bullet;
  19. [HideInInspector]
  20. public Transform spawn;
  21. void Start()
  22. {
  23. rb = GetComponent<Rigidbody2D>();
  24. anim = GetComponent<Animator>();
  25. }
  26. void Update()
  27. {
  28. moveInput = Input.GetAxis("Horizontal");
  29. transform.Translate(moveInput * speed, 0, 0);
  30. if (faceRight == false && moveInput < 0)
  31. {
  32. Flip();
  33. }
  34. else if (faceRight == true && moveInput > 0)
  35. {
  36. Flip();
  37. }
  38. if(Input.GetMouseButtonDown(0))
  39. {
  40. StartCoroutine(DelayAttack());
  41. }
  42. }
  43. IEnumerator DelayAttack()
  44. {
  45. Instantiate(Bullet, spawn.position, Quaternion.identity, this.transform);
  46. yield return new WaitForSeconds(0.3f);
  47. }
  48. public void Flip()
  49. {
  50. faceRight = !faceRight;
  51. Vector3 scaler = transform.localScale;
  52. scaler.x *= -1;
  53. transform.localScale = scaler;
  54. }
  55. }