Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Forked from Crushy/MoveTransform.cs
Created February 4, 2014 09:31
Show Gist options
  • Save keiranlovett/8800586 to your computer and use it in GitHub Desktop.
Save keiranlovett/8800586 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class MoveTransform : IActivated {
public Transform objectMoved;
public Movements activatedPos, deactivatedPos;
[System.Serializable]
public class Movements {
public Transform position;
public string startEvent;
public string endEvent;
public float timeTaken = 1;
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( activatedPos ) );
}
public override void DeActivate( GameObject deactivator ) {
StopAllCoroutines();
StartCoroutine( MoveTo( deactivatedPos ) );
}
private IEnumerator MoveTo(Movements mov) {
if ( !string.IsNullOrEmpty( mov.startEvent ) ) {
NotificationCentre.PostNotification( this, mov.startEvent );
}
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 );
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;
}
}
if ( !string.IsNullOrEmpty( mov.endEvent ) ) {
NotificationCentre.PostNotification( this, mov.endEvent );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment