Advertisement
annstasi

Arrow

Aug 16th, 2022
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Arrow : MonoBehaviour
  6. {
  7.     public float speed;
  8.     private Rigidbody2D rb;
  9.     public GameObject arrow;
  10.     public GameObject Player;
  11.     // Start is called before the first frame update
  12.     void Start()
  13.     {
  14.         rb = GetComponent<Rigidbody2D>();
  15.         // for (int i = 0; i < 5; i++)
  16.         // {
  17.         // Instantiate(arrow);
  18.         // }
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         rb.velocity = new Vector2(speed, 0);
  25.     }
  26.  
  27.     public void OnCollisionEnter2D(Collision2D coll)
  28.     {
  29.         if (coll.gameObject.tag == "End")
  30.             Destroy(gameObject);
  31.  
  32.             if (coll.gameObject.tag == "Player")
  33.             {
  34.                 // Уменьшаем здоровье игрока и удаляем стрелу
  35.                 Player.GetComponent<Player>().health -= 1;
  36.                 Destroy(gameObject);
  37.             }
  38.         }
  39.     }
  40.  
  41.  
  42. using System.Collections;
  43. using System.Collections.Generic;
  44. using UnityEngine;
  45.  
  46. public class ArrowSpawn : MonoBehaviour
  47. {
  48.     [SerializeField] private GameObject obj;
  49.     float y;
  50.     Vector2 whereToSpawn;
  51.     [SerializeField] private float spawnRate = 2f;
  52.     float nextSpawn = 0.0f;
  53.  
  54.     // Start is called before the first frame update
  55.     void Start()
  56.     {
  57.  
  58.     }
  59.  
  60.     // Update is called once per frame
  61.     void Update()
  62.     {
  63.         if (Time.time > nextSpawn)
  64.         {
  65.             nextSpawn = Time.time + spawnRate;
  66.             y = -6f;
  67.             whereToSpawn = new Vector2(transform.position.x, y);
  68.             Instantiate(obj, whereToSpawn, Quaternion.identity);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement