Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public abstract class Bewegung : MonoBehaviour
  6. {
  7.  
  8. public float laufZeit = 0.1f;
  9. public LayerMask gelaende;
  10.  
  11.  
  12. private BoxCollider2D boxCollider2D;
  13. private Rigidbody2D rb2d;
  14. private float berechnungGeschwindigkeit;
  15.  
  16. protected virtual void Start()
  17. {
  18. boxCollider2D = GetComponent<BoxCollider2D>();
  19. rb2d = GetComponent<Rigidbody2D>();
  20. berechnungGeschwindigkeit = 1f / laufZeit;
  21.  
  22.  
  23.  
  24. }
  25.  
  26. private void Update()
  27. {
  28.  
  29. }
  30.  
  31.  
  32. protected bool Bewegen(int xRichtung ,int yRichtung, out RaycastHit2D hinderniss)
  33. {
  34. Vector2 startPunkt = transform.position;
  35. Vector2 endPunkt = startPunkt + new Vector2(xRichtung, yRichtung);
  36.  
  37. boxCollider2D.enabled = false;
  38. hinderniss = Physics2D.Linecast(startPunkt, endPunkt, gelaende);
  39. boxCollider2D.enabled = true;
  40.  
  41. if(hinderniss.transform == null)
  42. {
  43. StartCoroutine(Laufen(endPunkt));
  44. return true;
  45. }
  46. return false;
  47. }
  48. protected IEnumerator Laufen (Vector3 ende)
  49. {
  50. //print(transform.position+ ende);
  51. float restlicheFelder = (transform.position - ende).sqrMagnitude;
  52.  
  53. // 0 kann auch float.Epsilon sein
  54. while (restlicheFelder > float.Epsilon)
  55. {
  56. //print(restlicheFelder + float.Epsilon);
  57. Vector3 neuePosition = Vector3.MoveTowards(transform.position, ende, berechnungGeschwindigkeit * Time.deltaTime);
  58. rb2d.MovePosition(neuePosition);
  59. restlicheFelder = (neuePosition - ende).sqrMagnitude;
  60. yield return null;
  61.  
  62. }
  63.  
  64. }
  65. protected virtual void LaufenVersuch<T>(int xRichtung ,int yRichtung)
  66. where T: Component
  67. {
  68. RaycastHit2D hinderniss;
  69. bool laufenMoeglich = Bewegen(xRichtung, yRichtung , out hinderniss);
  70.  
  71. if(hinderniss.transform == null)
  72. {
  73. return;
  74. }
  75. T hindernissBestanteil = hinderniss.transform.GetComponent<T>();
  76.  
  77. if (!laufenMoeglich && hindernissBestanteil != null)
  78. Stehen(hindernissBestanteil);
  79.  
  80. }
  81.  
  82. protected abstract void Stehen<T>(T bestandteil)
  83.  
  84. where T : Component;
  85.  
  86.  
  87.  
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement