Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class Bullet : MonoBehaviour
- {
- [SerializeField] private int _damage = 5;
- [SerializeField] private float _speed = 10;
- [SerializeField] private float _lifeTime = 3f;
- private Rigidbody _rigidbody;
- private void Awake()
- {
- _rigidbody = GetComponent<Rigidbody>();
- }
- public void Thorw()
- {
- _rigidbody.velocity = transform.forward * _speed;
- Destroy(gameObject, _lifeTime);
- }
- private void OnCollisionEnter(Collision other)
- {
- if (other.gameObject.TryGetComponent<Player>(out var player))
- {
- player.TakeDamage(_damage);
- Destroy(gameObject);
- }
- }
- }
Add Comment
Please, Sign In to add comment