using System.Collections; using System.Collections.Generic; using UnityEngine; public class MovingPlatform : MonoBehaviour { public ConditionCkecker conditionChecker; public Transform destination; public Transform origin; public float speed; private Transform endPoint; private float timer = 0; private bool positiveDirection; Dictionary<Collider, Transform> transformsParents = new Dictionary<Collider, Transform>(); // Start is called before the first frame update void Start() { positiveDirection = true; transform.position = origin.position; if (!positiveDirection) { endPoint = origin; } else { endPoint = destination; } } // Update is called once per frame void Update() { if (conditionChecker.CheckConditions()) { if (Vector3.Distance(transform.position, endPoint.position) > 0.1) { if (positiveDirection) { timer += Time.deltaTime * speed; } else { timer -= Time.deltaTime * speed; } transform.position = Vector3.MoveTowards(origin.position, destination.position, timer); } else { positiveDirection = !positiveDirection; if (!positiveDirection) { endPoint = origin; } else { endPoint = destination; } } } } public void OnReload() { timer = 0; endPoint = destination; } private void OnTriggerStay(Collider other) { if (!transformsParents.ContainsKey(other)) { transformsParents.Add(other, other.transform.parent); } if (other.transform.parent != transform) { other.transform.SetParent(transform); } } private void OnTriggerExit(Collider other) { Transform tr; if (transformsParents.TryGetValue(other,out tr)) { other.transform.SetParent(tr); transformsParents.Remove(other); } } }