Skip to content

Instantly share code, notes, and snippets.

@takoyakiroom
Created February 10, 2018 05:44
Show Gist options
  • Save takoyakiroom/014463064f3cda8ddeff0cead170ac49 to your computer and use it in GitHub Desktop.
Save takoyakiroom/014463064f3cda8ddeff0cead170ac49 to your computer and use it in GitHub Desktop.
ペン
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class Pen : MonoBehaviour {
public float LineDistance = 0.1f; // Trackを置き直す距離
public GameObject track; // 軌跡
VRTK_InteractableObject io; // コントローラ用
Vector3 start_pos; // 開始位置
GameObject current_track; // 先っちょ
GameObject pen_traks; // 軌跡置き場
void Start()
{
io = transform.parent.GetComponent<VRTK_InteractableObject>();
// 軌跡置き場
pen_traks = new GameObject("PenTracks");
pen_traks.transform.parent = null;
}
void Update()
{
// 使ってる?
if (io.IsUsing() == true)
{
// 一定距離動いた?
if (Vector3.Distance(start_pos, transform.position) > LineDistance)
{
// 開始位置保存
start_pos = transform.position;
if (current_track != null)
{
// 昔のやつを、軌跡を入れる用のオブジェクトに突っ込む
current_track.transform.parent = pen_traks.transform;
}
// 新しい軌跡を作成
current_track = Instantiate(track, transform.position, Quaternion.identity);
current_track.SetActive(true);
// 自分の子にする
current_track.transform.parent = transform;
}
}
else
{
// 線引いていた?
if (current_track != null)
{
// 昔のやつを、軌跡を入れる用のオブジェクトに突っ込む
current_track.transform.parent = pen_traks.transform;
current_track = null;
// 開始位置を遠くにしとく
start_pos = Vector3.one * 100f;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment