Skip to content

Instantly share code, notes, and snippets.

@CosmoCleaner
Last active August 29, 2015 14:15
Show Gist options
  • Save CosmoCleaner/0724cf0cd6bdc0002fc7 to your computer and use it in GitHub Desktop.
Save CosmoCleaner/0724cf0cd6bdc0002fc7 to your computer and use it in GitHub Desktop.
UnityでLineRendererを利用した斬撃の軌跡表示
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