add learning
This commit is contained in:
+77
-15
@@ -7,31 +7,93 @@ using UnityEngine;
|
||||
public class Train : MonoBehaviour
|
||||
{
|
||||
public TrainType type;
|
||||
public bool useEffect;
|
||||
private LocomotionHandler locomotionHandler;
|
||||
private Coroutine c_effect;
|
||||
private Material[] materials;
|
||||
private float effectDuration = .4f;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if(locomotionHandler == null)
|
||||
locomotionHandler = FindObjectOfType<LocomotionHandler>();
|
||||
if(materials == null)
|
||||
materials = GetComponent<MeshRenderer>().materials;
|
||||
|
||||
var startPosition = locomotionHandler.pullUpPositionBefore;
|
||||
var speed = locomotionHandler.trainSpeed;
|
||||
if(!useEffect)
|
||||
{
|
||||
if(locomotionHandler == null)
|
||||
locomotionHandler = FindObjectOfType<LocomotionHandler>();
|
||||
|
||||
transform.localPosition = new Vector3(transform.localPosition.x,
|
||||
transform.localPosition.y,
|
||||
startPosition);
|
||||
var startPosition = locomotionHandler.pullUpPositionBefore;
|
||||
var speed = locomotionHandler.trainSpeed;
|
||||
|
||||
transform.DOLocalMoveZ(-16, speed)
|
||||
.SetEase(Ease.OutSine);
|
||||
transform.localPosition = new Vector3(transform.localPosition.x,
|
||||
transform.localPosition.y,
|
||||
startPosition);
|
||||
|
||||
transform.DOLocalMoveZ(-16, speed)
|
||||
.SetEase(Ease.OutSine);
|
||||
}
|
||||
else SetEffect(true);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
var endPosition = locomotionHandler.pullUpPositionAfter;
|
||||
var speed = locomotionHandler.trainSpeed;
|
||||
if(!useEffect)
|
||||
{
|
||||
var endPosition = locomotionHandler.pullUpPositionAfter;
|
||||
var speed = locomotionHandler.trainSpeed;
|
||||
|
||||
transform.DOLocalMoveZ(endPosition, speed)
|
||||
.SetEase(Ease.InSine)
|
||||
.OnComplete(() => gameObject.SetActive(false));
|
||||
transform.DOLocalMoveZ(endPosition, speed)
|
||||
.SetEase(Ease.InSine)
|
||||
.OnComplete(() => gameObject.SetActive(false));
|
||||
}
|
||||
else SetEffect(false);
|
||||
}
|
||||
}
|
||||
|
||||
void SetEffect(bool isShow)
|
||||
{
|
||||
if(c_effect != null)
|
||||
{
|
||||
StopCoroutine(c_effect);
|
||||
c_effect = null;
|
||||
}
|
||||
|
||||
c_effect = StartCoroutine(SetEffectRoutine(isShow));
|
||||
}
|
||||
|
||||
IEnumerator SetEffectRoutine(bool isShow)
|
||||
{
|
||||
float startPosition = isShow ? -5 : 2;
|
||||
float currentValue = startPosition;
|
||||
float endPosition = 0;
|
||||
|
||||
if(isShow) endPosition = 2;
|
||||
else endPosition = -5;
|
||||
|
||||
float progress = 0;
|
||||
float duration = effectDuration;
|
||||
|
||||
while(progress < 1)
|
||||
{
|
||||
currentValue = Mathf.Lerp(startPosition, endPosition, progress);
|
||||
|
||||
for(int i = 0; i < materials.Length; i++)
|
||||
{
|
||||
materials[i]
|
||||
.SetFloat("_EffectPosition", currentValue);
|
||||
}
|
||||
|
||||
progress += duration * Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
for(int i = 0; i < materials.Length; i++)
|
||||
{
|
||||
materials[i]
|
||||
.SetFloat("_EffectPosition", endPosition);
|
||||
}
|
||||
|
||||
if(!isShow)
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user