Skip to content

Instantly share code, notes, and snippets.

@shundroid
Created October 4, 2015 05:01
Show Gist options
  • Save shundroid/ac7e0cbc77e6c75c229f to your computer and use it in GitHub Desktop.
Save shundroid/ac7e0cbc77e6c75c229f to your computer and use it in GitHub Desktop.
マウスでクリックしたところからScreenToRayを使わずにRayを発射する。
using UnityEngine;
using System.Collections;
public class Ray : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Vector3 mouse = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(mouse, new Vector3(0, 0, 1), 100);
// 可視化
Debug.DrawRay(mouse, new Vector3(0, 0, 100), Color.blue, 1);
// コンソール
Debug.Log(hit.collider);
}
}
}
@shundroid
Copy link
Author

なぜわざわざそうしたか:
2Dのゲームを作るときに、「○○の座標にgameObjectがあるか」を調べられるようにしたかったから。
mouse変数の部分を、好きな座標にすれば、「○○の座標にgameObjectがあるか」がわかる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment