Created
October 4, 2015 05:01
-
-
Save shundroid/ac7e0cbc77e6c75c229f to your computer and use it in GitHub Desktop.
マウスでクリックしたところからScreenToRayを使わずにRayを発射する。
This file contains 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; | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なぜわざわざそうしたか:
2Dのゲームを作るときに、「○○の座標にgameObjectがあるか」を調べられるようにしたかったから。
mouse変数の部分を、好きな座標にすれば、「○○の座標にgameObjectがあるか」がわかる。