|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PlatformMover : MonoBehaviour
- {
- [SerializeField] private Transform m_StartPos;
- [SerializeField] private Transform m_EndPos;
- [SerializeField] private float m_Duration = 2f;
-
- private Transform m_OldParent;
-
- private void Update()
- {
- transform.position = Vector2.Lerp(m_StartPos.position, m_EndPos.position, (Mathf.Sin(Time.time * m_Duration) + 1.0f) / 2.0f);
- }
-
- private void OnCollisionEnter2D(Collision2D collision)
- {
- var playerController = collision.gameObject.GetComponent<CharacterController2D>();
- if(playerController)
- {
- m_OldParent = playerController.transform.parent;
- playerController.transform.parent = transform;
- }
- }
-
- private void OnCollisionExit2D(Collision2D collision)
- {
- var playerController = collision.gameObject.GetComponent<CharacterController2D>();
- if(playerController)
- {
- playerController.transform.parent = m_OldParent;
- m_OldParent = null;
- }
-
- }
-
- #if UNITY_EDITOR
- private void OnDrawGizmos()
- {
- Gizmos.color = Color.green;
- Gizmos.DrawLine(m_StartPos.position, m_EndPos.position);
- }
- #endif
- }
|