Version: 5.3 (switch to 5.4b)
言語English
  • C#
  • JS

スクリプト言語

好きな言語を選択してください。選択した言語でスクリプトコードが表示されます。

Camera.ViewportPointToRay

マニュアルに切り替える
public Ray ViewportPointToRay(Vector3 position);

パラメーター

説明

ビューポート座標を介して、カメラからレイを飛ばします。

結果として得られたレイはワールド空間でカメラの近平面から始まり position の(x, y)はビューポート上のものと同じ座標値です。( position.z は無視されます)

ビューポート座標は正規化されカメラと関係しています。カメラの左下は (0, 0) で、右上が (1, 1)

	// Prints the name of the object camera is directly looking at
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { Camera camera; void Start() { camera = GetComponent<Camera>(); } void Update() { Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0)); RaycastHit hit; if (Physics.Raycast(ray, out hit)) print("I'm looking at " + hit.transform.name); else print("I'm looking at nothing!"); } }