Skip to content

Commit

Permalink
Push
Browse files Browse the repository at this point in the history
Whats happening
  • Loading branch information
aps16104 committed Apr 23, 2020
1 parent f9c628b commit be53b4d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
55 changes: 54 additions & 1 deletion Platformer/Assets/Scripts/Raycast/PlatformController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> passengerMovement; //store all the bools
Dictionary<Transform, Controller2D> passengerDictionary = new Dictionary<Transform, Controller2D>(); //to be able to jump

Expand Down Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions Platformer/Assets/_Scenes/Level1.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand Down

0 comments on commit be53b4d

Please sign in to comment.