Skip to content

Instantly share code, notes, and snippets.

@Crushy
Last active June 1, 2018 21:00
Show Gist options
  • Save Crushy/8760219 to your computer and use it in GitHub Desktop.
Save Crushy/8760219 to your computer and use it in GitHub Desktop.
Meant to be used with Easing functions. See http://easings.net/ for examples. Graphs should vary from x=0 to x=1.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MoveTransform : IActivated {
public Transform objectMoved;
public ColliderEnterExitMovements enter, leave;
[System.Serializable]
public class TimedEvent {
/// <summary>
/// Percentage of time this event will be launched at
/// </summary>
[Range(0,1)]
public float time = 0f;
public string name = "";
public bool localEvent = true;
}
[System.Serializable]
public class ColliderEnterExitMovements {
public Transform position;
public float timeTaken = 1;
public List<TimedEvent> events = new List<TimedEvent>();
public AnimationCurve xOffset = AnimationCurve.Linear( 0, 0, 1, 1 );
public AnimationCurve yOffset = AnimationCurve.Linear( 0, 0, 1, 1 );
public AnimationCurve zOffset = AnimationCurve.Linear( 0, 0, 1, 1 );
}
public override void Activate( GameObject activator ) {
StopAllCoroutines();
StartCoroutine( MoveTo( enter ) );
}
public override void DeActivate( GameObject deactivator ) {
StopAllCoroutines();
StartCoroutine( MoveTo( leave ) );
}
private IEnumerator MoveTo(ColliderEnterExitMovements mov) {
//Sort events by time
Queue<TimedEvent> unExpiredEvents = new Queue<TimedEvent>( mov.events.OrderBy( s => s.time ) );
if (mov.position!=null) {
Vector3 startPos = objectMoved.position;
float start = Time.timeSinceLevelLoad;
float end = start + mov.timeTaken;
float timePercentage;
Vector3 newPos;
for ( float currTime = Time.timeSinceLevelLoad; currTime < end; currTime = Time.timeSinceLevelLoad ) {
timePercentage = 1 - ( end - currTime ) / ( end - start );
//There's mid-animation events to process
ProcessEvents( unExpiredEvents, timePercentage );
newPos.x = startPos.x + ( mov.position.position.x - startPos.x ) * mov.xOffset.Evaluate( timePercentage );
newPos.y = startPos.y + ( mov.position.position.y - startPos.y ) * mov.yOffset.Evaluate( timePercentage );
newPos.z = startPos.z + ( mov.position.position.z - startPos.z ) * mov.zOffset.Evaluate( timePercentage );
objectMoved.position = newPos;
yield return null;
}
}
//We're done by default, launch all the remaining events
ProcessEvents( unExpiredEvents, 1f );
}
private void ProcessEvents(Queue<TimedEvent> unExpiredEvents,float currentTime) {
while ( unExpiredEvents.Count > 0 && unExpiredEvents.Peek().time<=currentTime) {
TimedEvent ev = unExpiredEvents.Dequeue();
if ( ev.localEvent )
SendMessage( ev.name );
else
NotificationCentre.PostNotification( this, ev.name );
}
}
}
@Crushy
Copy link
Author

Crushy commented Feb 18, 2014

Added support for events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment