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.

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