Skip to content

Instantly share code, notes, and snippets.

@takoyakiroom
Created August 24, 2016 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takoyakiroom/7f35b7da77e6f92955f4dabd6ba48bd2 to your computer and use it in GitHub Desktop.
Save takoyakiroom/7f35b7da77e6f92955f4dabd6ba48bd2 to your computer and use it in GitHub Desktop.
ドローン
using UnityEngine;
using System.Collections;
public class Coin : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider hit)
{
// DroneのGetCoinを呼び出す
GameObject drone = GameObject.Find("Drone");
Drone target = drone.GetComponent<Drone>();
target.GetCoin();
// 消える
Destroy(gameObject);
Debug.Log("OnTriggerEnter");
}
}
using UnityEngine;
using System.Collections;
public class Drone : MonoBehaviour
{
const float FLY_POW = 12.0f; // 飛ぶ時の力
const float MOVE_SPEED = 0.04f; // 移動速度
const float ROTATE_SPEED = 1.5f; // 回転速度
const float MOVE_TILT = 20f; // 移動した時の傾き
Rigidbody rb;
AudioSource[] audioSource;
float pow; // 飛ぶ力
Vector3 drc; // 向いてる方向
bool flying; // 飛んでる?
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = gameObject.GetComponents<AudioSource>();
}
// Update is called once per frame
void Update()
{
// 飛ぶ
rb.AddForce(0, pow * FLY_POW, 0);
// 飛んでる?
if (flying == true)
{
// 前後左右移動
transform.position += transform.forward * drc.y * MOVE_SPEED;
transform.position += transform.right * drc.x * MOVE_SPEED;
// 体を傾ける & 回る
transform.rotation = Quaternion.Euler(drc.y * MOVE_TILT, drc.z, drc.x * -MOVE_TILT);
}
}
// 飛ぶ
public void Power(float trg)
{
if (trg < 0.2f)
{
// 0.2以下は止まる
trg = 0;
}
pow = trg;
}
// 移動
public void Move(Vector2 pad)
{
if (System.Math.Abs(pad.y) > 0.2f)
{
drc.y = pad.y;
}
else
{
drc.y = 0;
}
if (System.Math.Abs(pad.x) > 0.2f)
{
drc.z += pad.x * ROTATE_SPEED;
}
}
// スライド
public void Slide(Vector2 pad)
{
if (System.Math.Abs(pad.x) > 0.2f)
{
drc.x = pad.x;
}
else
{
drc.x = 0;
}
}
// 落ちた
void OnCollisionEnter(Collision collision)
{
Debug.Log("OnCollisionEnter " + collision.gameObject.tag);
audioSource[0].Play();
}
// 地面についた
void OnCollisionStay(Collision collision)
{
Debug.Log("OnCollisionStay " + collision.gameObject.tag);
flying = false;
}
// 飛んだ
void OnCollisionExit(Collision collision)
{
Debug.Log("OnCollisionExit " + collision.gameObject.tag);
flying = true;
}
public void GetCoin()
{
audioSource[1].Play();
Debug.Log("GetCoin");
}
}
using UnityEngine;
using System.Collections;
public class Drone_ctrl : MonoBehaviour
{
//ボタンID
private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
private SteamVR_Controller.Device ctrl { get { return SteamVR_Controller.Input((int)trackedObj.index); } }
private SteamVR_TrackedObject trackedObj;
public Drone target; // 操作中のオブジェクト
public bool Move_ctrl; // 上下左右移動のコントローラ?
// Use this for initialization
void Start()
{
trackedObj = GetComponent<SteamVR_TrackedObject>();
var obj = GameObject.Find("Drone");
target = obj.GetComponent<Drone>();
}
// Update is called once per frame
void Update()
{
if (ctrl == null)
{
Debug.Log("コントローラがない");
return;
}
if (target == null)
{
Debug.Log("ドローンがない");
return;
}
if (Move_ctrl == true)
{
Move();
}
else
{
Slide();
}
}
// 移動用のコントローラ
void Move()
{
// ボタン取得
float value = ctrl.GetAxis(triggerButton).x;
Vector2 position = ctrl.GetAxis();
// ドローンに渡す
target.Power(value);
target.Move(position);
Debug.Log("power=" + value + " x,y=" + position.x + "," + position.y);
}
// スライド用のコントローラ
void Slide()
{
// ボタン取得
Vector2 position = ctrl.GetAxis();
// ドローンに渡す
target.Slide(position);
}
// テレポートは「Field」の「Teleportable.cs」
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment