Last active
December 29, 2019 13:43
-
-
Save botamochi6277/bac2ab71f3fbfe8bbcc65eeecec8271e to your computer and use it in GitHub Desktop.
LightSaber Lv01
This file contains hidden or 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; | |
| /// <sammary> | |
| /// オリジナルライトセイバーを実装する Lv01 | |
| /// </sammary> | |
| public class OVRLightSaber : CustomOVRGrabbable { | |
| protected PhysicalLaserPointer m_laser;// 光刃用クラス | |
| // 手離したら光刃を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>(); | |
| if (m_laser != null) { | |
| m_laser.enabled = false; | |
| m_laser.SetDistance(0f); | |
| } | |
| } | |
| override protected void Update() { | |
| base.Update(); | |
| // 掴まれている時 | |
| if (isGrabbed) { | |
| // 人差し指のボタンを押した時 | |
| if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, m_currentController)) { | |
| // 光刃をONにする | |
| if (m_laser != null) { | |
| m_laser.enabled = true; | |
| m_laser.SetDistance(1f); | |
| } | |
| } | |
| // 人差し指のボタンを離した時 | |
| if (OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger, m_currentController)) { | |
| if (m_laser != null) { | |
| m_laser.SetDistance(0f); | |
| m_laser.enabled = false; | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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 System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| /// <sammary> | |
| /// Raycast と LineRendererを統合したクラス | |
| /// </sammary> | |
| public class PhysicalLaserPointer : MonoBehaviour { | |
| // maximum distance of this laser | |
| [SerializeField] | |
| protected float m_distance = 100.0f; | |
| // offset of an emitting point | |
| [SerializeField] | |
| protected float m_offset = 0.0f; | |
| // Radius of this laser | |
| [SerializeField] | |
| protected float m_radius = 0.05f; | |
| // LineRenderer drawing this laser | |
| [SerializeField] | |
| protected LineRenderer m_lineRend; | |
| // Transform of an emitting point | |
| // The laser direction is Z-axis | |
| [SerializeField] | |
| protected Transform m_laserLightTf; | |
| // hit effect of this laser | |
| // this object must be a child of this object | |
| [SerializeField] | |
| protected GameObject m_hitEffect; | |
| // emitting effect of this laser | |
| // this object must be a child of this object | |
| [SerializeField] | |
| protected GameObject m_emitEffect; | |
| // laser power (offensive power) | |
| [SerializeField] | |
| protected int m_laserPower = 0; | |
| protected Transform m_hitObjTf; | |
| protected Ray m_ray; | |
| private ParticleSystem[] m_hitParticles = null; | |
| private ParticleSystem[] m_emitParticles = null; | |
| void Start() { | |
| m_ray = new Ray (m_laserLightTf.position, m_laserLightTf.forward); | |
| m_lineRend = gameObject.GetComponent<LineRenderer> (); | |
| if (m_lineRend != null) { | |
| m_lineRend.enabled = true; | |
| } | |
| if (m_laserLightTf == null) { | |
| m_laserLightTf = transform; | |
| } | |
| if (m_hitEffect != null) { | |
| m_hitParticles = m_hitEffect.GetComponentsInChildren<ParticleSystem>(); | |
| } | |
| if (m_emitEffect != null) { | |
| m_emitParticles = m_emitEffect.GetComponentsInChildren<ParticleSystem>(); | |
| } | |
| } | |
| // Update is called once per frame | |
| void Update() { | |
| RaycastHit hit; | |
| m_ray = new Ray (m_laserLightTf.position + m_offset * m_laserLightTf.forward, m_laserLightTf.forward); | |
| Debug.DrawLine (m_ray.origin, | |
| m_ray.origin + m_ray.direction * (m_distance - m_offset), | |
| Color.green); | |
| // Physics.SphereCast(m_ray, m_radius, out hit, m_distance) | |
| // Physics.Raycast(m_ray, out hit, m_distance) | |
| bool isHit; | |
| if (m_radius > 0.001f) { | |
| isHit = Physics.SphereCast(m_ray, m_radius, out hit, | |
| (m_distance - m_offset)); | |
| } else { | |
| isHit = Physics.Raycast(m_ray, out hit, m_distance - m_offset); | |
| } | |
| if (isHit) { | |
| // Hit` | |
| m_hitEffect.SetActive(true); | |
| m_hitEffect.transform.position = hit.point; // Pointer Particle | |
| m_hitEffect.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hit.normal); | |
| m_hitObjTf = hit.transform; | |
| // Debug.LogFormat("Laser hit on {0}",hit.collider.name); | |
| if (m_lineRend != null) { | |
| float dist = Vector3.Distance(m_ray.origin, hit.point); | |
| m_lineRend.SetPosition (0, m_ray.origin); | |
| // m_lineRend.SetPosition (1, hit.point); | |
| m_lineRend.SetPosition (1, m_ray.origin + m_laserLightTf.forward*dist); | |
| } | |
| // Beam Attsack | |
| if (m_laserPower != 0) { | |
| Damageable dmg = m_hitObjTf.gameObject.GetComponent<Damageable>(); | |
| if (dmg != null) { | |
| dmg.Injured(m_laserPower); | |
| } | |
| } | |
| } else { | |
| // reset position | |
| m_hitEffect.SetActive(false); | |
| if (m_lineRend != null) { | |
| m_lineRend.SetPosition (0, m_ray.origin); | |
| m_lineRend.SetPosition (1, m_ray.GetPoint(m_distance - m_offset)); | |
| } | |
| m_hitObjTf = null; | |
| } | |
| } | |
| void OnEnable () { | |
| if (m_lineRend != null) { | |
| m_lineRend.enabled = true; | |
| } | |
| if (m_emitEffect != null) { | |
| m_emitEffect.SetActive(true); | |
| } | |
| } | |
| void OnDisable () { | |
| if (m_lineRend != null) { | |
| m_lineRend.enabled = false; | |
| } | |
| if (m_hitEffect != null) { | |
| m_hitEffect.SetActive(false); | |
| } | |
| if (m_emitEffect != null) { | |
| m_emitEffect.SetActive(false); | |
| } | |
| } | |
| public void SetDistance(float newDistance) { | |
| m_distance = newDistance; | |
| } | |
| public Transform HitObjTf { | |
| get {return m_hitObjTf;} | |
| } | |
| public void SetColor(Color color) { | |
| if (m_lineRend != null) { | |
| m_lineRend.material.color = color; | |
| // for legacy particle shader | |
| if (m_lineRend.material.HasProperty("_TintColor")) { | |
| m_lineRend.material.SetColor("_TintColor", color); | |
| } | |
| } | |
| if (m_hitParticles != null) { | |
| for (int i = 0; i < m_hitParticles.Length; ++i) { | |
| m_hitParticles[i].startColor = color; | |
| } | |
| } | |
| } | |
| public float distance { | |
| get {return m_distance;} | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment