Skip to content

Instantly share code, notes, and snippets.

@komo91
Last active August 29, 2015 14:22
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 komo91/c2280c0fddcd3afda20d to your computer and use it in GitHub Desktop.
Save komo91/c2280c0fddcd3afda20d to your computer and use it in GitHub Desktop.
using UnityEngine; //使うライブラリの宣言
using System.Collections;
//MonoBehaviourを継承したPointigHandlerクラスの宣言
public class PointingHandler : MonoBehaviour {
//メンバーフィールドの宣言
public float distance = 20; //判定範囲
public GameObject ElbowRight; //右ひじ
public GameObject WristRight; //右手首
public GameObject ElbowLeft; //左ひじ
public GameObject WristLeft; //左手首
public LayerMask layerMask; //背景部分
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// 肘->手首 ベクトル (RAY) を算出
Ray rayRight = new Ray(WristRight.transform.position, WristRight.transform.position - ElbowRight.transform.position);
Ray rayLeft = new Ray(WristLeft.transform.position, WristLeft.transform.position - ElbowLeft.transform.position);
// 上記RAYがぶつかるオブジェクトを探索
FindAndPaint(rayRight); //FindAndPaintメソッドを呼び出し
FindAndPaint(rayLeft); //FindAndPaintメソッドを呼び出し
}
void FindAndPaint(Ray ray)
{
// ヒットした際の情報を格納するRaycastHitオブジェクト
RaycastHit hit;
// 腕差しベクトル、結果を収めるRaycastHitオブジェクト、判定範囲、レイヤーマスク
if (Physics.Raycast(ray, out hit, distance, layerMask))
{
// ヒットしたオブジェクトを特定して、色を付ける
hit.transform.gameObject.GetComponent<PaintIt>().setColor();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment