Meteorite.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Meteorite : MonoBehaviour
  5. {
  6. private AttackController ac;
  7. private Character owner;
  8. private Vector3 attackDir;
  9. private Rigidbody rb;
  10. public float speed;
  11. public int baseDamage;
  12. private int damage;
  13. private void Awake()
  14. {
  15. rb = GetComponent<Rigidbody>();
  16. ac = GetComponent<AttackController>();
  17. }
  18. public void Init(Vector3 targetPos,Character _owner)
  19. {
  20. owner = _owner;
  21. transform.position = CalculateSpawnPosition(targetPos);
  22. rb.velocity = speed * attackDir;
  23. damage = baseDamage + (int)((0.1f * GameManager.instance.armor + GameManager.instance.myTreasuresTag[3]) * baseDamage);
  24. }
  25. private void OnTriggerEnter(Collider other)
  26. {
  27. BeHitTrigger target = other.GetComponent<BeHitTrigger>();
  28. if (target != null)
  29. {
  30. if(target.owner is Enemy)
  31. {
  32. target.BeHit(ac.attackMethod_summon[0], owner);
  33. }
  34. }
  35. if (other.CompareTag("Ground"))
  36. {
  37. gameObject.SetActive(false);
  38. }
  39. }
  40. private Vector3 CalculateSpawnPosition(Vector3 targetPos)
  41. {
  42. float randomOffsetX = Random.Range(-8,8);
  43. Vector3 spawnPos = new Vector3(targetPos.x + randomOffsetX, /*Camera.main.ScreenToWorldPoint(new Vector3(0f, Screen.height)).y*/ targetPos.y + 15f, 0);
  44. attackDir = (targetPos - spawnPos).normalized;
  45. return spawnPos;
  46. }
  47. }