Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public abstract class Bewegung : MonoBehaviour
- {
- public float laufZeit = 0.1f;
- public LayerMask gelaende;
- private BoxCollider2D boxCollider2D;
- private Rigidbody2D rb2d;
- private float berechnungGeschwindigkeit;
- protected virtual void Start()
- {
- boxCollider2D = GetComponent<BoxCollider2D>();
- rb2d = GetComponent<Rigidbody2D>();
- berechnungGeschwindigkeit = 1f / laufZeit;
- }
- private void Update()
- {
- }
- protected bool Bewegen(int xRichtung ,int yRichtung, out RaycastHit2D hinderniss)
- {
- Vector2 startPunkt = transform.position;
- Vector2 endPunkt = startPunkt + new Vector2(xRichtung, yRichtung);
- boxCollider2D.enabled = false;
- hinderniss = Physics2D.Linecast(startPunkt, endPunkt, gelaende);
- boxCollider2D.enabled = true;
- if(hinderniss.transform == null)
- {
- StartCoroutine(Laufen(endPunkt));
- return true;
- }
- return false;
- }
- protected IEnumerator Laufen (Vector3 ende)
- {
- //print(transform.position+ ende);
- float restlicheFelder = (transform.position - ende).sqrMagnitude;
- // 0 kann auch float.Epsilon sein
- while (restlicheFelder > float.Epsilon)
- {
- //print(restlicheFelder + float.Epsilon);
- Vector3 neuePosition = Vector3.MoveTowards(transform.position, ende, berechnungGeschwindigkeit * Time.deltaTime);
- rb2d.MovePosition(neuePosition);
- restlicheFelder = (neuePosition - ende).sqrMagnitude;
- yield return null;
- }
- }
- protected virtual void LaufenVersuch<T>(int xRichtung ,int yRichtung)
- where T: Component
- {
- RaycastHit2D hinderniss;
- bool laufenMoeglich = Bewegen(xRichtung, yRichtung , out hinderniss);
- if(hinderniss.transform == null)
- {
- return;
- }
- T hindernissBestanteil = hinderniss.transform.GetComponent<T>();
- if (!laufenMoeglich && hindernissBestanteil != null)
- Stehen(hindernissBestanteil);
- }
- protected abstract void Stehen<T>(T bestandteil)
- where T : Component;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement