Global Game Jam 2022
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.

46 lines
1.3 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlatformMover : MonoBehaviour
  5. {
  6. [SerializeField] private Transform m_StartPos;
  7. [SerializeField] private Transform m_EndPos;
  8. [SerializeField] private float m_Duration = 2f;
  9. private Transform m_OldParent;
  10. private void Update()
  11. {
  12. transform.position = Vector2.Lerp(m_StartPos.position, m_EndPos.position, (Mathf.Sin(Time.time * m_Duration) + 1.0f) / 2.0f);
  13. }
  14. private void OnCollisionEnter2D(Collision2D collision)
  15. {
  16. var playerController = collision.gameObject.GetComponent<CharacterController2D>();
  17. if(playerController)
  18. {
  19. m_OldParent = playerController.transform.parent;
  20. playerController.transform.parent = transform;
  21. }
  22. }
  23. private void OnCollisionExit2D(Collision2D collision)
  24. {
  25. var playerController = collision.gameObject.GetComponent<CharacterController2D>();
  26. if(playerController)
  27. {
  28. playerController.transform.parent = m_OldParent;
  29. m_OldParent = null;
  30. }
  31. }
  32. #if UNITY_EDITOR
  33. private void OnDrawGizmos()
  34. {
  35. Gizmos.color = Color.green;
  36. Gizmos.DrawLine(m_StartPos.position, m_EndPos.position);
  37. }
  38. #endif
  39. }