You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Networking.Client;
  5. public class CannonShot : MonoBehaviour
  6. {
  7. public bool shootingRight;
  8. public ConnectedClients clientData;
  9. GameObject player;
  10. string charname;
  11. public List<string> Names;
  12. Rigidbody rb;
  13. private void Start()
  14. {
  15. rb = this.gameObject.GetComponent<Rigidbody>();
  16. for (int i = 0; i < clientData.AllClients.Count; i++)
  17. {
  18. Names.Add(clientData.AllClients[i].characterAnimal + "(Clone)");
  19. }
  20. }
  21. IEnumerator PushLeftCoroutine(float time)
  22. {
  23. float elapsedTime = 0;
  24. Vector3 startPosition = transform.position;
  25. Vector3 endPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1.0f);
  26. time *= 0.8f;
  27. yield return new WaitForSeconds(0.05f);
  28. while (elapsedTime < time)
  29. {
  30. transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  31. yield return new WaitForEndOfFrame();
  32. elapsedTime += Time.deltaTime;
  33. }
  34. transform.position = endPosition;
  35. Destroy(gameObject);
  36. }
  37. IEnumerator PushRightCoroutine(float time)
  38. {
  39. float elapsedTime = 0;
  40. Vector3 startPosition = transform.position;
  41. Vector3 endPosition = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1.0f);
  42. time *= 0.8f;
  43. yield return new WaitForSeconds(0.05f);
  44. while (elapsedTime < time)
  45. {
  46. transform.position = Vector3.Lerp(startPosition, endPosition, (elapsedTime / time));
  47. yield return new WaitForEndOfFrame();
  48. elapsedTime += Time.deltaTime;
  49. }
  50. transform.position = endPosition;
  51. Destroy(gameObject);
  52. }
  53. void OnTriggerEnter(Collider other)
  54. {
  55. foreach (string name in Names)
  56. {
  57. if (other.gameObject.name == name)
  58. {
  59. rb.velocity = Vector3.zero;
  60. player = GameObject.Find(charname);
  61. Rigidbody playerRB = player.GetComponent<Rigidbody>();
  62. if (shootingRight == true)
  63. {
  64. StartCoroutine(PushRightCoroutine(0.5f));
  65. player.GetComponent<Character>().CannonRMove(0.5f);
  66. }
  67. else
  68. {
  69. StartCoroutine(PushLeftCoroutine(0.5f));
  70. player.GetComponent<Character>().CannonLMove(0.5f);
  71. }
  72. }
  73. }
  74. }
  75. }