Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save peroon/15a68ab84416de7c654d to your computer and use it in GitHub Desktop.

Select an option

Save peroon/15a68ab84416de7c654d to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
// カメラからレイを飛ばす
public class RayShooter : MonoBehaviour {
public LayerMask hitLayerMask; // レイ判定するオブジェクトは限定する
public float rayLength = 20.0f;
private Ray ray;
private RaycastHit raycastHit;
void Update () {
// Debug Line
Debug.DrawLine(transform.position, transform.position + transform.forward * rayLength, Color.red);
// 毎フレーム、向きに応じてレイを作る
ray = new Ray(transform.position, transform.forward);
// ヒットしたオブジェクトのイベントを呼ぶ
if (Physics.Raycast (ray, out raycastHit, rayLength, hitLayerMask)) {
//Debug.Log(raycastHit.collider.name);
raycastHit.collider.SendMessage("OnRayHit");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment