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

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Camera.ViewportPointToRay

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

Parameters

Description

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

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

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

no example available in JavaScript
	// 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!"); } }