Anonim_999

WHAAAAA

May 31st, 2024
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.76 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using TMPro;
  6.  
  7. public class CoinCounter : MonoBehaviour
  8. {
  9.     [SerializeField] private TMP_Text _coinText;
  10.  
  11.     private void Awake()
  12.     {
  13.         _coinText.text = "Coins: 1";
  14.     }
  15.  
  16.     public void UpdateCoins(int amount)
  17.     {
  18.         _coinText.text = $"Coins: {amount}";
  19.     }
  20. }
  21.  
  22. using System.Collections;
  23. using System.Collections.Generic;
  24. using UnityEngine;
  25.  
  26. public class DataHandler : MonoBehaviour
  27. {
  28.     public static string Horizontal = "Horizontal";
  29.     public static string Jump = "Jump";
  30.     public static int DeathLimit = 3;
  31.        
  32. }
  33.  
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using UnityEngine;
  37.  
  38. public class Enemy : MonoBehaviour
  39. {
  40.     [SerializeField] private string _name = "Enemy";
  41.     [SerializeField] private float _damage = 10;
  42.    
  43.     private Animator _animator;
  44.     private SpriteRenderer _spriteRenderer;
  45.     private float _maxHealth;
  46.     private ParticleSystem _particleSystem;
  47.    
  48.     private void Start()
  49.     {
  50.         _animator = GetComponent<Animator>();
  51.         _particleSystem = GetComponentInChildren<ParticleSystem>();
  52.     }
  53.  
  54.     private void OnCollisionEnter2D(Collision2D collision)
  55.     {
  56.         if (collision.transform.TryGetComponent(out Player player) & collision.transform.TryGetComponent(out HealthBar healthBar))
  57.         {
  58.             healthBar.GetDamage(_damage);
  59.             healthBar.UpdateHealthBar();
  60.             Rigidbody2D rigidbody2D = player.GetComponent<Rigidbody2D>();
  61.             Vector2 direction = (player.transform.position - transform.position).normalized;
  62.             rigidbody2D.AddForce(direction * 2f, ForceMode2D.Force);
  63.         }
  64.     }
  65. }
  66.  
  67. using System.Collections;
  68. using UnityEngine;
  69.  
  70. public class EnemyMovement : MonoBehaviour
  71. {
  72.     [SerializeField] private float _forceJump;
  73.     [SerializeField] private float _delay;
  74.  
  75.     private Rigidbody2D _rigidbody2D;
  76.     private bool _canJump = true;
  77.  
  78.     private void Start()
  79.     {
  80.         _rigidbody2D = GetComponent<Rigidbody2D>();
  81.     }
  82.  
  83.     private void FixedUpdate()
  84.     {
  85.         if (_canJump)
  86.         {
  87.             StartCoroutine(JumpWithDelay());
  88.         }
  89.     }
  90.  
  91.     private IEnumerator JumpWithDelay()
  92.     {
  93.         _canJump = false;
  94.         Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 5f);
  95.         bool isFound = false;
  96.         WaitForSeconds waitForSeconds = new WaitForSeconds(_delay);
  97.  
  98.         foreach (var collider in colliders)
  99.         {
  100.             if (collider.TryGetComponent(out Player player))
  101.             {
  102.                 isFound = true;
  103.                 Vector2 direction = (player.transform.position - transform.position).normalized;
  104.                 _rigidbody2D.AddForce((direction + new Vector2(0,_forceJump)), ForceMode2D.Impulse);
  105.                 transform.localScale = new Vector3(-direction.x >= 0 ? 1 : -1, transform.localScale.y, transform.localScale.z);
  106.             }
  107.         }
  108.  
  109.         if (isFound == false)
  110.         {
  111.             Vector2 direction = new Vector2((Random.Range(-1, 1) == 0 ? 1f : -1f), 1f);
  112.             _rigidbody2D.AddForce(direction * _forceJump, ForceMode2D.Impulse);
  113.         }
  114.         yield return waitForSeconds;
  115.         _canJump = true;
  116.     }
  117. }
  118.  
  119.  
  120. using System;
  121. using System.Collections;
  122. using System.Collections.Generic;
  123. using TMPro;
  124. using UnityEngine;
  125. using UnityEngine.SceneManagement;
  126. using Random = UnityEngine.Random;
  127.  
  128. public abstract class Health : MonoBehaviour
  129. {
  130.     [SerializeField] protected float _maxHealth;
  131.     [SerializeField] protected GameObject _coinPrefab;
  132.     [SerializeField] private TMP_Text _livesText;
  133.     [SerializeField] private bool _isHero = false;
  134.     [SerializeField] private AudioClip _clip;
  135.     [SerializeField] private AudioSource _audioSource;
  136.     protected float _curentHealth;
  137.     private static int _deathLimit = 3;
  138.    
  139.     private void Awake()
  140.     {
  141.         _curentHealth = _maxHealth;
  142.         _livesText.text = $"Lives: {_deathLimit}";
  143.     }
  144.  
  145.     public void GetDamage(float damage)
  146.     {
  147.         _curentHealth -= damage;
  148.        
  149.         if (_curentHealth < 0 && _isHero)
  150.         {
  151.             Death();
  152.         }
  153.         else if (_curentHealth < 0)
  154.         {
  155.             _audioSource.clip = _clip;
  156.             _audioSource.Play();
  157.             gameObject.SetActive(false);
  158.         }
  159.     }
  160.  
  161.     public void Death()
  162.     {
  163.         _audioSource.clip = _clip;
  164.         _audioSource.Play();
  165.        
  166.         if (_deathLimit <= 0)
  167.         {
  168.             SetHealth(DataHandler.DeathLimit);
  169.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  170.         }
  171.         else
  172.         {
  173.             gameObject.SetActive(false);
  174.             _deathLimit -= 1;
  175.             _livesText.text = $"Lives: {_deathLimit}";
  176.             transform.position = Player.GetStartPosition();
  177.             _curentHealth = _maxHealth;
  178.             gameObject.SetActive(true);
  179.         }
  180.     }
  181.    
  182.     public void Heal(float heal)
  183.     {
  184.         _curentHealth += heal;
  185.        
  186.         if (_curentHealth > _maxHealth)
  187.         {
  188.             _curentHealth = _maxHealth;
  189.         }
  190.     }
  191.    
  192.     public static int GetSavedDeathLimit()
  193.     {
  194.         return _deathLimit;
  195.     }
  196.  
  197.     public static void SetHealth(int amount)
  198.     {
  199.         _deathLimit = amount;
  200.     }
  201. }
  202.  
  203. using TMPro;
  204. using UnityEngine;
  205.  
  206. public class HealthBar : Health
  207. {
  208.     [SerializeField] private SpriteRenderer _healthBarImage;
  209.     [SerializeField] private Color _color = Color.green;
  210.    
  211.     private void Start()
  212.     {
  213.         _healthBarImage.color = _color;
  214.     }
  215.    
  216.     public void UpdateHealthBar()
  217.     {
  218.         float fillAmount = _curentHealth / _maxHealth;
  219.         _healthBarImage.transform.localScale = new Vector2(fillAmount, 1f);
  220.     }
  221. }
  222.  
  223. using System;
  224. using System.Collections;
  225. using System.Collections.Generic;
  226. using UnityEngine;
  227. using TMPro;
  228.  
  229. public class KeyCollector : MonoBehaviour
  230. {
  231.     [SerializeField] private int _neededKeys = 3;
  232.     [SerializeField] private TMP_Text _counterKeys;  
  233.     private int _currentKeys = 0;
  234.  
  235.     private void Start()
  236.     {
  237.         _counterKeys.text = $"Keys: {_currentKeys}";
  238.     }
  239.  
  240.     private void OnTriggerEnter2D(Collider2D other)
  241.     {
  242.         if (other.transform.TryGetComponent(out Key key))
  243.         {
  244.             _currentKeys += 1;
  245.             _counterKeys.text = $"Keys: {_currentKeys}";
  246.             Destroy(key.gameObject);
  247.         }
  248.     }
  249.  
  250.     public bool CheckAccess()
  251.     {
  252.         if (_currentKeys >= _neededKeys)
  253.             return true;
  254.         return false;
  255.     }
  256. }
  257.  
  258. using System;
  259. using System.Collections;
  260. using System.Collections.Generic;
  261. using UnityEngine;
  262. using UnityEngine.SceneManagement;
  263.  
  264. public class PortalKeyChecker : MonoBehaviour
  265. {
  266.     [SerializeField] private int _indexScene = 0;
  267.    
  268.     private void OnTriggerEnter2D(Collider2D other)
  269.     {
  270.         if (other.transform.TryGetComponent(out KeyCollector keyCollector))
  271.         {
  272.             if (keyCollector.CheckAccess())
  273.             {
  274.                 SceneManager.LoadScene(_indexScene);
  275.             }
  276.         }
  277.     }
  278. }
  279.  
  280.  
  281. using System.Collections;
  282. using System.Collections.Generic;
  283. using Unity.VisualScripting;
  284. using UnityEngine;
  285. using UnityEngine.SceneManagement;
  286.  
  287. public class CorrectRestart : MonoBehaviour
  288. {
  289.     public void Restart()
  290.     {
  291.         Health.SetHealth(DataHandler.DeathLimit);
  292.     }
  293. }
  294.  
  295. using System.Collections;
  296. using System.Collections.Generic;
  297. using UnityEngine;
  298. using UnityEngine.SceneManagement;
  299.  
  300. public class MenuButtons : MonoBehaviour
  301. {
  302.     [SerializeField] private GameObject _menuPanel;
  303.     [SerializeField] private GameObject _optionsPanel;
  304.     [SerializeField] private int _sceneIndex = 0;
  305.    
  306.     public void Exit()
  307.     {
  308.         Application.Quit();
  309.     }
  310.  
  311.     public void Play()
  312.     {
  313.         SceneManager.LoadScene(_sceneIndex);
  314.     }
  315.  
  316.     public void OpenOptions()
  317.     {
  318.         if (_menuPanel != null)
  319.         {
  320.             _menuPanel.SetActive(false);
  321.         }
  322.  
  323.         if (_optionsPanel != null)
  324.         {
  325.             _optionsPanel.SetActive(true);
  326.         }
  327.     }
  328.  
  329.     public void Back()
  330.     {
  331.         if (_menuPanel != null)
  332.         {
  333.             _menuPanel.SetActive(true);
  334.         }
  335.  
  336.         if (_optionsPanel != null)
  337.         {
  338.             _optionsPanel.SetActive(false);
  339.         }
  340.     }
  341. }
  342.  
  343.  
  344. using System.Collections;
  345. using System.Collections.Generic;
  346. using UnityEngine;
  347.  
  348. public class MenuKeyBinds : MonoBehaviour
  349. {
  350.     [SerializeField] private GameObject _menu;
  351.    
  352.     private void Update()
  353.     {
  354.         if (Input.GetKeyDown(KeyCode.Escape))
  355.         {
  356.             if (_menu.activeSelf == false)
  357.             {
  358.                 _menu.SetActive(true);
  359.             }
  360.             else
  361.             {
  362.                 _menu.SetActive(false);
  363.             }
  364.         }
  365.     }
  366. }
  367.  
  368.  
  369. using UnityEngine;
  370. using UnityEngine.UI;
  371.  
  372. public class MusicSlider : MonoBehaviour
  373. {
  374.     [SerializeField] private AudioSource _audioSource;
  375.     private Slider _slider;
  376.     private static float _savedValume = 0.5f;
  377.    
  378.     private void Start()
  379.     {
  380.         _slider = GetComponent<Slider>();
  381.         _slider.value = _audioSource.volume;
  382.         _savedValume = _audioSource.volume;
  383.     }
  384.  
  385.     private void OnEnable()
  386.     {
  387.         _slider.value = _audioSource.volume;
  388.     }
  389.  
  390.     public void UpdateVolume()
  391.     {
  392.         _audioSource.volume = _slider.value;
  393.         _savedValume = _audioSource.volume;
  394.     }
  395.  
  396.     public static float GetSavedValume()
  397.     {
  398.         return _savedValume;
  399.     }
  400. }
  401.  
  402.  
  403. using System;
  404. using System.Collections;
  405. using System.Collections.Generic;
  406. using UnityEngine;
  407.  
  408. public class AIEnabler : MonoBehaviour
  409. {
  410.     private void OnTriggerEnter2D(Collider2D other)
  411.     {
  412.         if (other.transform.TryGetComponent(out EnemyMovement enemyMovement))
  413.         {
  414.             if (enemyMovement.enabled == false)
  415.             {
  416.                 enemyMovement.enabled = true;
  417.             }
  418.         }
  419.     }
  420.  
  421.     private void OnTriggerExit2D(Collider2D other)
  422.     {
  423.         if (other.transform.TryGetComponent(out EnemyMovement enemyMovement))
  424.         {
  425.             if (enemyMovement.enabled == true)
  426.             {
  427.                 enemyMovement.enabled = false;
  428.             }
  429.         }
  430.     }
  431. }
  432.  
  433.  
  434. using System;
  435. using TMPro;
  436. using Unity.VisualScripting;
  437. using UnityEngine;
  438.  
  439. [RequireComponent(typeof(SpriteRenderer), typeof(HealthBar))]
  440. public class Player : MonoBehaviour
  441. {
  442.     private static Vector3 _startPosition;
  443.     private HealthBar _healthBar;
  444.  
  445.     private void Awake()
  446.     {
  447.         _healthBar = GetComponent<HealthBar>();
  448.         _startPosition = transform.position;
  449.     }
  450.  
  451.     public static Vector3 GetStartPosition()
  452.     {
  453.         return _startPosition;
  454.     }
  455. }
  456.  
  457.  
  458. using System.Collections;
  459. using System.Collections.Generic;
  460. using UnityEngine;
  461.  
  462. [RequireComponent(typeof(CircleCollider2D))]
  463. public class PlayerAttack : MonoBehaviour
  464. {
  465.     [SerializeField] private float _attackSpeed;
  466.     [SerializeField] private float _timeBeforeAttack = .33f;
  467.     [SerializeField] private float _timeAfterAttack = .67f;
  468.     [SerializeField] private Animator _animator;
  469.     [SerializeField] private float _damage = 12;
  470.     [SerializeField] private float _attackForce;
  471.     [SerializeField] private List<AudioClip> _attackSounds;
  472.     [SerializeField] private AudioSource _audioSource;
  473.  
  474.     private bool _canAttack = true;
  475.     private float _attackTime;
  476.     private int _attackState;
  477.     private CircleCollider2D _circleCollider2D;
  478.  
  479.     private float _tempBeforeAttack;
  480.     private float _tempAfterAttack;
  481.  
  482.     private void Start()
  483.     {
  484.         _circleCollider2D = gameObject.GetComponent<CircleCollider2D>();
  485.         _tempBeforeAttack = _timeBeforeAttack;
  486.         _tempAfterAttack = _timeAfterAttack;
  487.     }
  488.  
  489.     private void Update()
  490.     {
  491.         if (Input.GetKey(KeyCode.Mouse0) && _canAttack)
  492.         {
  493.             StartCoroutine(AttackWithDelay());
  494.         }
  495.  
  496.         if (Input.GetKeyUp(KeyCode.Mouse0))
  497.         {
  498.             _animator.SetBool("Attack", false);
  499.         }
  500.     }
  501.  
  502.     private void Attack()
  503.     {
  504.         Collider2D[] hits = Physics2D.OverlapCircleAll(_circleCollider2D.transform.position + new Vector3(_circleCollider2D.offset.x, _circleCollider2D.offset.y, 0) * transform.localScale.x, _circleCollider2D.radius);
  505.        
  506.         if (_attackSounds.Count > 0 && _audioSource != null)
  507.         {
  508.             AudioClip randomClip = _attackSounds[Random.Range(0, _attackSounds.Count)];
  509.             _audioSource.clip = randomClip;
  510.             _audioSource.Play();
  511.         }
  512.        
  513.         foreach (var collider in hits)
  514.         {
  515.             if (collider.TryGetComponent(out Enemy enemy) & collider.TryGetComponent(out HealthBar healthBar))
  516.             {
  517.                 healthBar.GetDamage(_damage);
  518.                 healthBar.UpdateHealthBar();
  519.                 Rigidbody2D rigidbody2D = enemy.GetComponent<Rigidbody2D>();
  520.                 Vector2 direction = (enemy.transform.position - transform.position).normalized;
  521.                 rigidbody2D.AddForce(new Vector2(direction.x * _attackForce, _attackForce), ForceMode2D.Impulse);
  522.             }
  523.         }
  524.     }
  525.  
  526.     private IEnumerator AttackWithDelay()
  527.     {
  528.         _canAttack = false;
  529.         float attackSpeed = 1f + (_attackSpeed / 100);
  530.         _timeBeforeAttack = (_tempBeforeAttack / attackSpeed);
  531.         _timeAfterAttack = (_tempAfterAttack / attackSpeed);
  532.         _attackTime = _timeBeforeAttack + _timeAfterAttack;
  533.         _animator.SetFloat("AttackSpeed", attackSpeed);
  534.         _animator.SetBool("Attack", true);
  535.         WaitForSeconds waitForSeconds = new WaitForSeconds(_timeBeforeAttack);
  536.         yield return waitForSeconds;
  537.         Attack();
  538.         _canAttack = true;
  539.     }
  540. }
  541.  
  542.  
  543. using System.Collections.Generic;
  544. using UnityEngine;
  545.  
  546. public class PlayerMovement : MonoBehaviour
  547. {
  548.     [SerializeField] private float _minGroundNormalY = .65f;
  549.     [SerializeField] private float _gravityModifier = 1f;
  550.     [SerializeField] private Vector2 _velocity;
  551.     [SerializeField] private LayerMask _layerMask;
  552.     [SerializeField] private float _speed;
  553.     [SerializeField] private float _jumpPower;  
  554.  
  555.     protected Vector2 TargetVelocity;
  556.     protected bool Grounded;
  557.     protected Vector2 GroundNormal;
  558.     protected Rigidbody2D RigidBody;
  559.     protected ContactFilter2D ContactFilter;
  560.     protected RaycastHit2D[] HitBuffer = new RaycastHit2D[16];
  561.     protected List<RaycastHit2D> HitBufferList = new List<RaycastHit2D>(16);
  562.  
  563.     protected const float minMoveDistance = 0.001f;
  564.     protected const float shellRadius = 0.01f;
  565.  
  566.     private Animator _animator;
  567.     private bool isFacingRight = true;
  568.  
  569.     void OnEnable()
  570.     {
  571.         RigidBody = GetComponent<Rigidbody2D>();
  572.     }
  573.  
  574.     void Start()
  575.     {
  576.         ContactFilter.useTriggers = false;
  577.         ContactFilter.SetLayerMask(_layerMask);
  578.         ContactFilter.useLayerMask = true;
  579.         _animator = GetComponent<Animator>();
  580.     }
  581.  
  582.     void Update()
  583.     {
  584.         TargetVelocity = new Vector2(Input.GetAxis("Horizontal") * _speed, 0);
  585.  
  586.         if (TargetVelocity.x != 0 && Grounded)
  587.         {
  588.             _animator.SetBool("Jump",false);
  589.             _animator.SetBool("Run", true);
  590.         }
  591.         else if (TargetVelocity.x == 0 && Grounded)
  592.         {
  593.             _animator.SetBool("Jump",false);
  594.             _animator.SetBool("Run",false);
  595.             _animator.SetBool("Idle",true);
  596.         }
  597.  
  598.         if (Input.GetKey(KeyCode.Space) && Grounded)
  599.         {
  600.             _velocity.y = _jumpPower;
  601.             _animator.SetBool("Run",false);
  602.             _animator.SetBool("Idle",false);
  603.             _animator.SetBool("Jump",true);
  604.         }
  605.     }
  606.  
  607.     void FixedUpdate()
  608.     {
  609.         _velocity += _gravityModifier * Physics2D.gravity * Time.deltaTime;
  610.         _velocity.x = TargetVelocity.x;
  611.  
  612.         Grounded = false;
  613.  
  614.         Vector2 deltaPosition = _velocity * Time.deltaTime;
  615.         Vector2 moveAlongGround = new Vector2(GroundNormal.y, -GroundNormal.x);
  616.         Vector2 move = moveAlongGround * deltaPosition.x;
  617.  
  618.         Movement(move, false);
  619.  
  620.         move = Vector2.up * deltaPosition.y;
  621.  
  622.         Movement(move, true);
  623.         Flip();
  624.     }
  625.  
  626.     void Movement(Vector2 move, bool yMovement)
  627.     {
  628.         float distance = move.magnitude;
  629.  
  630.         if (distance > minMoveDistance)
  631.         {
  632.             int count = RigidBody.Cast(move, ContactFilter, HitBuffer, distance + shellRadius);
  633.  
  634.             HitBufferList.Clear();
  635.  
  636.             for (int i = 0; i < count; i++)
  637.             {
  638.                 HitBufferList.Add(HitBuffer[i]);
  639.             }
  640.  
  641.             for (int i = 0; i < HitBufferList.Count; i++)
  642.             {
  643.                 Vector2 currentNormal = HitBufferList[i].normal;
  644.                 if (currentNormal.y > _minGroundNormalY)
  645.                 {
  646.                     Grounded = true;
  647.                     _animator.SetBool("Idle",true);
  648.  
  649.                     if (yMovement)
  650.                     {
  651.                         GroundNormal = currentNormal;
  652.                         currentNormal.x = 0;
  653.                     }
  654.                 }
  655.  
  656.                 float projection = Vector2.Dot(_velocity, currentNormal);
  657.                 if (projection < 0)
  658.                 {
  659.                     _velocity = _velocity - projection * currentNormal;
  660.                 }
  661.  
  662.                 float modifiedDistance = HitBufferList[i].distance - shellRadius;
  663.                 distance = modifiedDistance < distance ? modifiedDistance : distance;
  664.             }
  665.         }
  666.  
  667.         RigidBody.position = RigidBody.position + move.normalized * distance;
  668.     }
  669.  
  670.     private void Flip()
  671.     {
  672.         if (isFacingRight && TargetVelocity.x < 0f || !isFacingRight && TargetVelocity.x > 0f)
  673.         {
  674.             isFacingRight = !isFacingRight;
  675.             Vector3 localScale = transform.localScale;
  676.             localScale.x *= -1f;
  677.             transform.localScale = localScale;
  678.         }
  679.     }
  680. }
  681.  
  682.  
  683. using System;
  684. using System.Collections;
  685. using System.Collections.Generic;
  686. using Unity.VisualScripting;
  687. using UnityEngine;
  688.  
  689. [RequireComponent(typeof(AudioSource))]
  690. public class LoadMusic : MonoBehaviour
  691. {
  692.     private AudioSource _audioSource;
  693.  
  694.     private void Start()
  695.     {
  696.         _audioSource = GetComponent<AudioSource>();
  697.         _audioSource.volume = MusicSlider.GetSavedValume();
  698.     }
  699. }
  700.  
  701.  
  702. using System;
  703. using System.Collections;
  704. using System.Collections.Generic;
  705. using TMPro;
  706. using UnityEngine;
  707. using UnityEngine.SceneManagement;
  708.  
  709. public class CaptionsAnimate : MonoBehaviour
  710. {
  711.     [SerializeField] private TMP_Text _text;
  712.     [SerializeField] private float _speed = 1f;
  713.     [SerializeField] private float _targetY = 100;
  714.  
  715.     private void Start()
  716.     {
  717.         StartCoroutine(MoveTextUp());
  718.     }
  719.  
  720.     private IEnumerator MoveTextUp()
  721.     {
  722.         float currentY = _text.rectTransform.anchoredPosition.y;
  723.        
  724.         while (currentY < _targetY)
  725.         {
  726.             currentY += _speed * Time.deltaTime;
  727.             _text.rectTransform.anchoredPosition = new Vector2(_text.rectTransform.anchoredPosition.x, currentY);
  728.             yield return null;
  729.         }
  730.  
  731.         SceneManager.LoadScene(0);
  732.     }
  733. }
  734.  
  735.  
  736. using System;
  737. using System.Collections;
  738. using System.Collections.Generic;
  739. using Unity.VisualScripting;
  740. using UnityEngine;
  741.  
  742. public class DeathBox : MonoBehaviour
  743. {
  744.     private void OnTriggerEnter2D(Collider2D other)
  745.     {
  746.         if (other.transform.TryGetComponent(out Player player))
  747.         {
  748.             HealthBar healthBar = player.gameObject.GetComponent<HealthBar>();
  749.            
  750.             if (healthBar != null)
  751.             {
  752.                 healthBar.Death();
  753.                 healthBar.UpdateHealthBar();
  754.             }
  755.         }
  756.         else
  757.         {
  758.             Destroy(other.transform.gameObject);
  759.         }
  760.     }
  761. }
  762.  
  763.  
  764. using System;
  765. using System.Collections;
  766. using System.Collections.Generic;
  767. using Unity.VisualScripting;
  768. using UnityEngine;
  769.  
  770. public class ParalaxBackground : MonoBehaviour
  771. {
  772.     [SerializeField] private Transform _target;
  773.     [SerializeField, Range(0f, 1f)] private float _strength = 0.1f;
  774.     [SerializeField] private bool _isVerticalParalax;
  775.  
  776.     private Vector3 _targetPreviousPosition;
  777.  
  778.     private void Start()
  779.     {
  780.         if (!_target)
  781.         {
  782.             _target = Camera.main.transform;
  783.         }
  784.  
  785.         _targetPreviousPosition = _target.position;
  786.     }
  787.  
  788.     private void Update()
  789.     {
  790.         Vector3 delta = _target.position - _targetPreviousPosition;
  791.  
  792.         if (_isVerticalParalax == false)
  793.         {
  794.             delta.y = 0;
  795.         }
  796.  
  797.         _targetPreviousPosition = _target.position;
  798.         transform.position += delta * _strength;
  799.     }
  800. }
  801.  
Advertisement
Add Comment
Please, Sign In to add comment