Browse Source

Platform now moves player

jeff
Jeff 2 years ago
parent
commit
f1e07f2128
2 changed files with 24 additions and 8 deletions
  1. BIN
      Assets/Scenes/Level 2.unity
  2. +22
    -6
      Assets/Scripts/PlatformMover.cs

BIN
Assets/Scenes/Level 2.unity (Stored with Git LFS) View File

size 441461

+ 22
- 6
Assets/Scripts/PlatformMover.cs View File

@ -8,16 +8,32 @@ public class PlatformMover : MonoBehaviour
[SerializeField] private Transform m_EndPos;
[SerializeField] private float m_Duration = 2f;
// Start is called before the first frame update
void Start()
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);
}
// Update is called once per frame
void Update()
private void OnCollisionEnter2D(Collision2D collision)
{
transform.position = Vector2.Lerp(m_StartPos.position, m_EndPos.position, (Mathf.Sin(Time.time * m_Duration) + 1.0f) / 2.0f);
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

Loading…
Cancel
Save