Skip to content

Instantly share code, notes, and snippets.

@botamochi6277
Created January 10, 2020 05:57
Show Gist options
  • Save botamochi6277/2d7156fa528344685eda2f80622695b8 to your computer and use it in GitHub Desktop.
Save botamochi6277/2d7156fa528344685eda2f80622695b8 to your computer and use it in GitHub Desktop.
LightSaber Lv02
using UnityEngine;
/// <sammary>
/// オリジナルライトセイバーを実装する Lv02
/// </sammary>
public class OVRLightSaber : CustomOVRGrabbable {
[SerializeField]
protected GameObject m_blade; // 光刃 z方向に長い
protected PhysicalLaserPointer m_laser;// 光刃(LineRenderer based)用クラス
// Sound Parameters
protected AudioSource m_audioSource;
[SerializeField]
protected AudioClip m_turnOnSound;
[SerializeField]
protected AudioClip m_turnOffSound;
// Blade の長さを設定する
void SetBladeLength(float length) {
if (m_laser != null) {
m_laser.SetDistance(length);
if (length < 0.01f) {
m_laser.enabled = false;
} else {
m_laser.enabled = true;
}
}
if (m_blade != null) {
m_blade.transform.localScale = new Vector3(1f, 1f, length);
}
}
// 手離したら光刃をOFFにする
public override void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity) {
base.GrabEnd(linearVelocity, angularVelocity);
if (m_laser != null) {
m_laser.enabled = false;
}
}
// PhysicalLaserPointerを取得してOFFにする
override protected void Start() {
base.Start();
m_laser = GetComponent<PhysicalLaserPointer>();
m_audioSource = GetComponent<AudioSource>();
SetBladeLength(0f);
}
override protected void Update() {
base.Update();
// 掴まれている時
if (isGrabbed) {
// 人差し指のボタンを押した時
if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, m_currentController)) {
// 光刃をONにする
if ((m_audioSource != null) &&
(m_turnOnSound != null)) {
m_audioSource.PlayOneShot(m_turnOnSound, 1.0f);
}
iTween.ValueTo(gameObject,
iTween.Hash("from", 0f,
"to", 1f,
"time", 0.5f,
"onupdate", "SetBladeLength"));
}
// 人差し指のボタンを離した時
if (OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger, m_currentController)) {
// 光刃をOFFにする
if ((m_audioSource != null) &&
(m_turnOffSound != null)) {
m_audioSource.PlayOneShot(m_turnOffSound, 1.0f);
}
iTween.ValueTo(gameObject,
iTween.Hash("from", 1f,
"to", 0f,
"time", 0.5f,
"onupdate", "SetBladeLength"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment