Pro_Unit

Bullet

May 10th, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using System;
  2.  
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. public class Bullet : MonoBehaviour
  7. {
  8.     [SerializeField] private int _damage = 5;
  9.     [SerializeField] private float _speed = 10;
  10.     [SerializeField] private float _lifeTime = 3f;
  11.     private Rigidbody _rigidbody;
  12.  
  13.     private void Awake()
  14.     {
  15.         _rigidbody = GetComponent<Rigidbody>();
  16.     }
  17.     public void Thorw()
  18.     {
  19.         _rigidbody.velocity = transform.forward * _speed;
  20.         Destroy(gameObject, _lifeTime);
  21.     }
  22.  
  23.     private void OnCollisionEnter(Collision other)
  24.     {
  25.         if (other.gameObject.TryGetComponent<Player>(out var player))
  26.         {
  27.             player.TakeDamage(_damage);
  28.             Destroy(gameObject);
  29.         }
  30.     }
  31. }
Add Comment
Please, Sign In to add comment