Last active
May 31, 2021 09:53
-
-
Save tsubaki/4f293e0186afc33ebaa5 to your computer and use it in GitHub Desktop.
Unityで座標を等速直線的に動かすやつ。コメントアウト解除するとカーブエディタで他の移動パターンも作れる
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; | |
using System.Collections; | |
public class Liner : MonoBehaviour | |
{ | |
[SerializeField, Range(0, 10)] | |
float time = 1; | |
[SerializeField] | |
Vector3 endPosition; | |
//[SerializeField] | |
//AnimationCurve curve; | |
private float startTime; | |
private Vector3 startPosition; | |
void OnEnable () | |
{ | |
if (time <= 0) { | |
transform.position = endPosition; | |
enabled = false; | |
return; | |
} | |
startTime = Time.timeSinceLevelLoad; | |
startPosition = transform.position; | |
} | |
void Update () | |
{ | |
var diff = Time.timeSinceLevelLoad - startTime; | |
if (diff > time) { | |
transform.position = endPosition; | |
enabled = false; | |
} | |
var rate = diff / time; | |
//var pos = curve.Evaluate(rate); | |
transform.position = Vector3.Lerp (startPosition, endPosition, rate); | |
//transform.position = Vector3.Lerp (startPosition, endPosition, pos); | |
} | |
void OnDrawGizmosSelected () | |
{ | |
#if UNITY_EDITOR | |
if( !UnityEditor.EditorApplication.isPlaying || enabled == false ){ | |
startPosition = transform.position; | |
} | |
UnityEditor.Handles.Label(endPosition, endPosition.ToString()); | |
UnityEditor.Handles.Label(startPosition, startPosition.ToString()); | |
#endif | |
Gizmos.DrawSphere (endPosition, 0.1f); | |
Gizmos.DrawSphere (startPosition, 0.1f); | |
Gizmos.DrawLine (startPosition, endPosition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment