言語: 日本語
  • C#
  • JS
  • Boo

スクリプト言語

お好みのスクリプト言語を選択すると、サンプルコードがその言語で表示されます。

Physics.Raycast

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 static function Raycast(origin: Vector3, direction: Vector3, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool Raycast(Vector3 origin, Vector3 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
public static def Raycast(origin as Vector3, direction as Vector3, distance as float = Mathf.Infinity, layerMask as int = DefaultRaycastLayers) as bool

Parameters

origin ワールド座標でのレイの開始地点
direction レイの方向
distance レイの距離
layerMask レイヤマスクの設定。特定のオブジェクトとの衝突を無視することもできる

Returns

bool レイが任意のコライダーと交わる場合はtrue、それ以外はfalse

Description

シーン内のコライダーに対してレイを飛ばし、ヒットした情報を取得します

	function Update () {
		var fwd = transform.TransformDirection (Vector3.forward);
		if (Physics.Raycast (transform.position, fwd, 10)) {
			print ("There is something in front of the object!");
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        if (Physics.Raycast(transform.position, fwd, 10))
            print("There is something in front of the object!");
        
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Update() as void:
		fwd as Vector3 = transform.TransformDirection(Vector3.forward)
		if Physics.Raycast(transform.position, fwd, 10):
			print('There is something in front of the object!')

注意: この関数は球の中から外に向けてレイキャストすると false を返しますが、 それは意図通りの動作です。 If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a Raycast will hit the collider at it's new position.

public static function Raycast(origin: Vector3, direction: Vector3, hitInfo: RaycastHit, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool Raycast(Vector3 origin, Vector3 direction, RaycastHit hitInfo, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
public static def Raycast(origin as Vector3, direction as Vector3, hitInfo as RaycastHit, distance as float = Mathf.Infinity, layerMask as int = DefaultRaycastLayers) as bool

Parameters

origin ワールド座標でのレイの開始地点
direction レイの方向
distance レイの距離
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
layerMask レイヤマスクの設定。特定のオブジェクトとの衝突を無視することもできる

Returns

bool レイが任意のコライダーと交わる場合はtrue、それ以外はfalse

Description

シーン内の全てのコライダーに対してレイを飛ばし、ヒットした情報を取得します

	function Update () {
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
			var distanceToGround = hit.distance;
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, out hit))
            float distanceToGround = hit.distance;
        
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Update() as void:
		hit as RaycastHit
		if Physics.Raycast(transform.position, -Vector3.up, ):
			distanceToGround as float = hit.distance

他の例:

	// Raycast up to 100 meters down

	function Update () {
		var hit : RaycastHit;
		if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
			var distanceToGround = hit.distance;
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, out hit, 100.0F))
            float distanceToGround = hit.distance;
        
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Update() as void:
		hit as RaycastHit
		if Physics.Raycast(transform.position, -Vector3.up, , 100.0F):
			distanceToGround as float = hit.distance

public static function Raycast(ray: Ray, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool Raycast(Ray ray, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
public static def Raycast(ray as Ray, distance as float = Mathf.Infinity, layerMask as int = DefaultRaycastLayers) as bool

Parameters

ray レイの始点と方向
distance レイの距離
layerMask レイヤマスクの設定。特定のオブジェクトとの衝突を無視することもできる

Returns

bool レイが任意のコライダーと交わる場合はtrue、それ以外はfalse

Description

上記と同様ですが /ray.origin/ と /ray.direction/ を、 origin および direction の代わりに使用します

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

	if (Physics.Raycast (ray, 100)) {
		print ("Hit something");
	}
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, 100))
            print("Hit something");
        
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Update() as void:
		ray as Ray = Camera.main.ScreenPointToRay(Input.mousePosition)
		if Physics.Raycast(ray, 100):
			print('Hit something')

public static function Raycast(ray: Ray, hitInfo: RaycastHit, distance: float = Mathf.Infinity, layerMask: int = DefaultRaycastLayers): bool;
public static bool Raycast(Ray ray, RaycastHit hitInfo, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);
public static def Raycast(ray as Ray, hitInfo as RaycastHit, distance as float = Mathf.Infinity, layerMask as int = DefaultRaycastLayers) as bool

Parameters

ray レイの始点と方向
distance レイの距離
hitInfo If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
layerMask レイヤマスクの設定。特定のオブジェクトとの衝突を無視することもできる

Returns

bool レイが任意のコライダーと交わる場合はtrue、それ以外はfalse

Description

上記と同様ですが /ray.origin/ と /ray.direction/ を、 origin および direction の代わりに使用します

function Update() {
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;
	if (Physics.Raycast (ray, hit, 100)) {
		Debug.DrawLine (ray.origin, hit.point);
	}
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 100))
            Debug.DrawLine(ray.origin, hit.point);
        
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Update() as void:
		ray as Ray = Camera.main.ScreenPointToRay(Input.mousePosition)
		hit as RaycastHit
		if Physics.Raycast(ray, , 100):
			Debug.DrawLine(ray.origin, hit.point)