Moving platforms
This commit is contained in:
parent
4416f83b03
commit
aabe5457e2
32 changed files with 3169 additions and 47 deletions
58
Assets/Scripts/Obstacles/MovingPlatform.cs
Normal file
58
Assets/Scripts/Obstacles/MovingPlatform.cs
Normal file
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using SimpleTools;
|
||||
using UnityEngine;
|
||||
|
||||
public class MovingPlatform : MonoBehaviour {
|
||||
|
||||
[SerializeField] Vector2 endPosition;
|
||||
[SerializeField] float moveTime;
|
||||
[SerializeField] float stopDuration;
|
||||
|
||||
Vector2 _start, _end;
|
||||
float _speed;
|
||||
|
||||
Coroutine _swapRoutine;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Awake() {
|
||||
_start = transform.position;
|
||||
_end = _start + endPosition;
|
||||
_speed = endPosition.magnitude / moveTime;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate() {
|
||||
StartCoroutine(FixedUpdateCoroutine());
|
||||
}
|
||||
|
||||
IEnumerator FixedUpdateCoroutine() {
|
||||
yield return new WaitForFixedUpdate();
|
||||
transform.position = Vector3.MoveTowards(transform.position, _end, _speed * Time.deltaTime);
|
||||
|
||||
if (Math.Abs(transform.position.sqrMagnitude - _end.sqrMagnitude) < 0.05f) {
|
||||
_swapRoutine ??= StartCoroutine(SwapCoordinates());
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator SwapCoordinates() {
|
||||
yield return new WaitForSeconds(stopDuration);
|
||||
(_start, _end) = (_end, _start);
|
||||
_swapRoutine = null;
|
||||
}
|
||||
|
||||
void OnTriggerEnter2D(Collider2D col) {
|
||||
if (col.CompareTag("Player")) col.transform.SetParent(transform);
|
||||
}
|
||||
void OnTriggerExit2D(Collider2D col) {
|
||||
if(col.CompareTag("Player")) col.transform.SetParent(null);
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine(transform.position, transform.position + (Vector3)endPosition);
|
||||
Gizmos.DrawSphere(transform.position, 0.25f);
|
||||
Gizmos.DrawSphere(transform.position + (Vector3)endPosition, 0.25f);
|
||||
}
|
||||
}
|
11
Assets/Scripts/Obstacles/MovingPlatform.cs.meta
Normal file
11
Assets/Scripts/Obstacles/MovingPlatform.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9dd2f647ee7b470488f36b4c923f42e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue