Last active
August 29, 2015 14:15
-
-
Save CosmoCleaner/0724cf0cd6bdc0002fc7 to your computer and use it in GitHub Desktop.
UnityでLineRendererを利用した斬撃の軌跡表示
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; | |
/// <summary> | |
/// 剣の軌跡を生成 | |
/// </summary> | |
public class SwordTrack : MonoBehaviour | |
{ | |
/// <summary> | |
/// 軌跡を表示する剣先スピードしきい値 | |
/// </summary> | |
public float speedThreshold = 0.1f; | |
/// <summary> | |
/// キャラクターのアニメーション | |
/// 待機状態"idle"を検知するのに使用 | |
/// </summary> | |
public Animator anim; | |
LineRenderer line; | |
Vector3 oldPos; | |
int vertexIndex = 0; | |
#region unity | |
void Awake() | |
{ | |
line = GetComponent<LineRenderer>(); | |
oldPos = transform.position; | |
} | |
void Update() | |
{ | |
Vector3 diffVec = transform.position - oldPos; | |
if (diffVec.magnitude > speedThreshold && !IsNextIdle()) | |
{ | |
// 剣の軌跡描画処理 | |
line.SetVertexCount(vertexIndex + 1); | |
line.SetPosition(vertexIndex++, transform.position); | |
} | |
else | |
{ | |
vertexIndex = 0; | |
} | |
oldPos = transform.position; | |
} | |
#endregion | |
/// <summary> | |
/// 次のアニメーションステートが"idle"か否か | |
/// </summary> | |
bool IsNextIdle() | |
{ | |
return anim.GetNextAnimatorStateInfo(0).IsName("idle"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment