본문 바로가기
XR/Unity3D

Unity - Raycast로 앞의 가까운 object 찾기

by -솔솔- 2018. 9. 30.
반응형

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class raycast_hand : MonoBehaviour {

public Transform target_wall;


// Use this for initialization

void Start () {


}


// Update is called once per frame

void Update () {

RaycastHit hit;

Debug.DrawRay(transform.position, transform.forward * 2.0f, Color.red);


if(Physics.Raycast(transform.position, transform.forward, out hit, 2.0f))

{

target_wall = hit.transform;


Vector3 incomingVec = hit.point - transform.position;


//hit.point: the impact point in world space where the ray hit the collider.

                        Debug.DrawLine(transform.position, hit.point, Color.red); //ray

//hit.normal: the normal of the surface the ray hit.

                        Debug.DrawRay(hit.point, hit.normal, Color.green); //wall surface normal

//hit.distance //The distance from the ray's origin to the impact point.

Debug.DrawRay (transform.position, transform.forward * hit.distance, Color.blue);

}

}

}



댓글