diff --git a/Platformer/Assets/Scripts/Raycast/PlatformController.cs b/Platformer/Assets/Scripts/Raycast/PlatformController.cs index ad87b46..35e5777 100644 --- a/Platformer/Assets/Scripts/Raycast/PlatformController.cs +++ b/Platformer/Assets/Scripts/Raycast/PlatformController.cs @@ -6,11 +6,20 @@ public class PlatformController : RaycastController { public LayerMask passengerMask; //layer used for passengers - public Vector3 move; public Vector3[] localWaypoints;//relative to platform Vector3[] globalWaypoints; + public float speed; //speed of platform + public bool cyclic; //cycles + public float waitTime; //how long to wait + [Range(0,2)] + public float easeAmount; // how much to ease + + int fromWaypointIndex; //global waypoint were moving away from + float percentBetweenWaypoints; //percent moved between waypoints (0-1) + + float nextMoveTime; //next time to move List passengerMovement; //store all the bools Dictionary passengerDictionary = new Dictionary(); //to be able to jump @@ -38,6 +47,50 @@ void Update() MovePassengers(false);//Dont move } + float Ease(float x) + { + float a = easeAmount + 1; + return Mathf.Pow(x, a) / (Mathf.Pow(x, a) + Mathf.Pow(1 - x, a)); + } + + + Vector3 CalculatePlatformMovement() + { + + if (Time.time < nextMoveTime) //dont move + { + return Vector3.zero; + } + + fromWaypointIndex %= globalWaypoints.Length; //reset to 0 each time it reaches + int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length; //to the next waypoint + float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]); //distance between + percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints; //increase % each frame + percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints); //clamp between 0 and 1 + float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints); //percent eased + + Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints); //new position + + if (percentBetweenWaypoints >= 1) //reached or passed point + { + percentBetweenWaypoints = 0; //reset % + fromWaypointIndex++; //increment index + + if (!cyclic) //if not cyclic + { + if (fromWaypointIndex >= globalWaypoints.Length - 1) //reached end + { + fromWaypointIndex = 0; //start again + System.Array.Reverse(globalWaypoints); //reverse through waypoints + } + } + nextMoveTime = Time.time + waitTime; //set next time to move + } + + return newPos - transform.position; //amount we want to move this frame + } + + void MovePassengers(bool beforeMovePlatform) //before we move the platform { foreach (PassengerMovement passenger in passengerMovement) //for every movement diff --git a/Platformer/Assets/_Scenes/Level1.unity b/Platformer/Assets/_Scenes/Level1.unity index 7ff1f1e..bd67e7c 100644 --- a/Platformer/Assets/_Scenes/Level1.unity +++ b/Platformer/Assets/_Scenes/Level1.unity @@ -40256,7 +40256,7 @@ GameObject: - component: {fileID: 1553272772} - component: {fileID: 1553272771} - component: {fileID: 1553272770} - - component: {fileID: 1553272774} + - component: {fileID: 1553272773} m_Layer: 9 m_Name: Moving m_TagString: Untagged @@ -40352,7 +40352,7 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &1553272774 +--- !u!114 &1553272773 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -40367,18 +40367,16 @@ MonoBehaviour: collisionMask: serializedVersion: 2 m_Bits: 0 - horizontalRayCount: 8 - verticalRayCount: 10 + horizontalRayCount: 4 + verticalRayCount: 4 horizontalRaySpacing: 0 verticalRaySpacing: 0 collider: {fileID: 0} passengerMask: serializedVersion: 2 - m_Bits: 256 - move: {x: 0, y: 1.5, z: 0} - localWaypoints: - - {x: 0, y: 18.61, z: 0} - - {x: 0, y: 0, z: 0} + m_Bits: 0 + move: {x: 0, y: 0, z: 0} + localWaypoints: [] --- !u!1 &1628328973 GameObject: m_ObjectHideFlags: 0