Koha9
a80f3d6560
Initial commit. Complete project for 'I Wanna Be A Fox' - a 2D scrolling game inspired by 'I Wanna Be The Guy'.
124 lines
3.2 KiB
C#
124 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class TogeMoveLevel2 : RaycastController
|
|
{
|
|
|
|
public LayerMask passengerMask;
|
|
|
|
public Vector3[] localWaypoints;
|
|
Vector3[] globalWaypoints;
|
|
|
|
public float speed;
|
|
public bool cyclic;
|
|
public float waitTime;
|
|
[Range(0, 2)]
|
|
public float easeAmount;
|
|
|
|
int fromWaypointIndex;
|
|
float percentBetweenWaypoints;
|
|
float nextMoveTime;
|
|
bool MoveBool = false;
|
|
|
|
Dictionary<Transform, Controller2D> passengerDictionary = new Dictionary<Transform, Controller2D>();
|
|
|
|
public override void Start()
|
|
{
|
|
base.Start();
|
|
|
|
globalWaypoints = new Vector3[localWaypoints.Length];
|
|
for (int i = 0; i < localWaypoints.Length; i++)
|
|
{
|
|
globalWaypoints[i] = localWaypoints[i] + transform.position;
|
|
}
|
|
}
|
|
|
|
public void DoMoveLevel2()
|
|
{
|
|
MoveBool = true;
|
|
}
|
|
|
|
public void DoNotMoveLevel2()
|
|
{
|
|
MoveBool = false;
|
|
}
|
|
|
|
|
|
void Update()
|
|
{
|
|
|
|
if (MoveBool == true)
|
|
{
|
|
UpdateRaycastOrigins();
|
|
|
|
Vector3 velocity = CalculatePlatformMovement();
|
|
|
|
|
|
|
|
transform.Translate(velocity);
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
return Vector3.zero;
|
|
}
|
|
|
|
fromWaypointIndex %= globalWaypoints.Length;
|
|
int toWaypointIndex = (fromWaypointIndex + 1) % globalWaypoints.Length;
|
|
float distanceBetweenWaypoints = Vector3.Distance(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex]);
|
|
percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
|
|
percentBetweenWaypoints = Mathf.Clamp01(percentBetweenWaypoints);
|
|
float easedPercentBetweenWaypoints = Ease(percentBetweenWaypoints);
|
|
|
|
Vector3 newPos = Vector3.Lerp(globalWaypoints[fromWaypointIndex], globalWaypoints[toWaypointIndex], easedPercentBetweenWaypoints);
|
|
|
|
if (percentBetweenWaypoints >= 1)
|
|
{
|
|
percentBetweenWaypoints = 0;
|
|
fromWaypointIndex++;
|
|
|
|
if (!cyclic)
|
|
{
|
|
if (fromWaypointIndex >= globalWaypoints.Length - 1)
|
|
{
|
|
fromWaypointIndex = 0;
|
|
System.Array.Reverse(globalWaypoints);
|
|
}
|
|
}
|
|
nextMoveTime = Time.time + waitTime;
|
|
}
|
|
|
|
return newPos - transform.position;
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
if (localWaypoints != null)
|
|
{
|
|
Gizmos.color = Color.red;
|
|
float size = .3f;
|
|
|
|
for (int i = 0; i < localWaypoints.Length; i++)
|
|
{
|
|
Vector3 globalWaypointPos = (Application.isPlaying) ? globalWaypoints[i] : localWaypoints[i] + transform.position;
|
|
Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
|
|
Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
|
|
}
|
|
}
|
|
}
|
|
|
|
} |