Created
July 7, 2020 17:22
-
-
Save vysinsky/80fc7bd3c8faa116fad0dd8a7afc54c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace Animations | |
{ | |
public class PositionTransition | |
{ | |
private readonly float _duration; | |
private readonly Vector3 _from; | |
private readonly Vector3 _to; | |
private float _animationTime; | |
public PositionTransition(Vector3 from, Vector3 to, float duration) | |
{ | |
_from = from; | |
_to = to; | |
_duration = duration; | |
} | |
public Vector3 GetCurrentPosition(float deltaTime) | |
{ | |
_animationTime += deltaTime / _duration; | |
return Vector3.Lerp( | |
_from, | |
_to, | |
_animationTime | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using Animations; | |
using UnityEngine; | |
namespace Behaviours | |
{ | |
public class PositionTransitionBehaviour : MonoBehaviour | |
{ | |
private float _animationTime; | |
private PositionTransition _positionTransition; | |
[Tooltip("Animation delay in seconds")] | |
public float animationDelay; | |
[Tooltip("Animation duration in seconds")] | |
public float animationDuration; | |
public Vector3 startingPositionOffset; | |
private void Awake() | |
{ | |
var position = transform.position; | |
_positionTransition = new PositionTransition( | |
position + startingPositionOffset, | |
position, | |
animationDuration | |
); | |
} | |
private void Start() | |
{ | |
transform.position = _positionTransition.GetCurrentPosition(0f); | |
StartCoroutine(InitiateTiming()); | |
enabled = false; | |
} | |
private void Update() | |
{ | |
transform.position = _positionTransition.GetCurrentPosition(Time.deltaTime); | |
} | |
private IEnumerator InitiateTiming() | |
{ | |
yield return new WaitForSeconds(animationDelay); | |
enabled = true; | |
yield return new WaitForSeconds(animationDuration); | |
enabled = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment