Unity Raycast - 2018-Interface-Programming-Exhibition/6team-Arduino_Game_Controller GitHub Wiki
์์์ ๋ถํฐ ์ง์ ํ ๋ฐฉํฅ์ผ๋ก ํฅํ๋ ๋ฌดํ๋์ ์ , ์ด๋ฅผ ์ด์ฉํด ์ํ๋ ๋ฐฉํฅ๊ณผ ๊ฑฐ๋ฆฌ์ ์กด์ฌํ๋ ๋ฌผ์ฒด๋ฅผ ํ์ธํ๋ค.
ํ์ํ ๋ณ์ : ๋ฐฉํฅ, ์์์
physics์ ๋ฉ์๋๋ก ray๊ฐ ์ถฉ๋์ฒด์ ์ถฉ๋ํ๋ฉด True๋ฅผ ์๋๋ฉด False๋ฅผ ๋ฐํํ๋ค. ์ฃผ๋ก ์ถฉ๋์ฒด์ ์ด๋ฆ๊ณผ ์์น๋ฅผ ์ป๊ธฐ์ํด ์ฌ์ฉํ๋ค.
ํ์ํ ๋ณ์ : ์์์ , ๋ฐฉํฅ, ์ถฉ๋์ฒด(RaycastHit), ์ต๋๊ฑฐ๋ฆฌ, ๋ฌด์ํ ์ถฉ๋์ฒด, queryTriggerInteraction (์์ ๋ณ์๋ฅผ ๋ชจ๋ ํ์๋ก ํ์ง๋ ์์)
Debug์ ๋ฉ์๋๋ก, ์ง์ ํ ์์์ ๊ณผ ๋ฒกํฐ๋งํผ ์ ์ ์ด์ด ๋ณด์ฌ์ค๋ค ์ด๋ ํ์ธ์ ์ํ๊ฒ์ผ๋ก ๊ฒ์ํ๋ฉด์๋ ๋ณด์ด์ง ์๊ณ , ์ฝ์์ฐฝ์ด๋ ์ฌ๋ทฐ์์๋ง ํ์ธํ ์ ์๋ค.
ํ์ํ ๋ณ์ : ์์์ , ๋ฒกํฐ, ์ ์ ์, ๋ณด์ฌ์ง๋ ์๊ฐ, depthTest
duration : How long the line will be visible for (in seconds). depthTest : Should the line be obscured by other objects closer to the camera?
๊ทธ๋ํฝํจ๊ณผ๋ฅผ ์ํด ์ฐ์ด๋ lineRenderer์ ์ ์ ๊ทธ๋ฆฌ๋๋ฐ ์ฐ์ธ๋ค. ์ด๊ฒ ๋ง๊ณ ๋ ์ค๋ธ์ ํธ๊ฐ ์ง๋๊ฐ ๊ธธ์ ์๋ ํธ๋ ์ผ๋ ๋๋ฌ ๋ฑ ๋ง์ ํจ๊ณผ๊ฐ ์๋ค.
๋ lineRenderer.positionCount๋ ๋ผ์ธ๋ ๋๋ฌ๋ฅผ ์ด์ฉํด ๋ง๋ค์ด์ง ์ ์ ์ธ๊ทธ๋จผํธ(๊บฝ์ฌ์ง๋ ํ์) ์๋ฅผ ์ง์ ํ๋ค(๋ํ ์ฝ์ด์จ๋ค.)
ray์ ์ถฉ๋ํ ๋ฌผ์ฒด๋ฅผ ์ฝ์ด์ฌ ๋ ์ ์ฉํ๋ค. ์๋ ์ฝ๋ if๋ฌธ์ ์กฐ๊ฑด์ผ๋ก Physics.Raycast(ray.origin, ray.direction, out hit, 100) ๊ฐ ์๋๋ฐ, out hit ์ฒ๋ผ ์ฐ์ด๊ณ ๊ทธ ์กฐ๊ฑด๋ฌธ ๋ด๋ถ์์ ์ถฉ๋์ฒด์ ์ ๋ณด๋ฅผ ์ฝ์ด์ฌ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋๋ค.
using System.Collections.Generic;
using UnityEngine;
public class RayEmitter : MonoBehaviour {
Transform goTransform;
LineRenderer lineRenderer;
Ray ray;
RaycastHit hit; //์ถฉ๋์ฒด
Vector3 inDirection;
int nReflections = 2;
int nPoints;
void Start () {
goTransform = GetComponent<Transform>();
Debug.Log(goTransform.position);
lineRenderer = GetComponent<LineRenderer>();
}
void Update () {
nReflections =100;
ray = new Ray(goTransform.position, goTransform.forward);
Debug.DrawRay(goTransform.position, goTransform.forward * 100, Color.yellow);//์๋น์ ์ด๋ฏ๋ก ํ๋ ์ด์ด์ ํ๋ฉด์์๋ ๋ณด์ด์ง ์์
nPoints = 2;
lineRenderer.positionCount = nPoints;
lineRenderer.SetPosition(0, goTransform.position);
for (int i = 0; i <= nReflections; i++) //์ด๋ฅผ ๋ฐ๋ณต๋ฌธ์ ํตํด ๋ฐ์ฌ๋ ์ ์๋ ํ์๋ฅผ ๊ฒฐ์
{
if (i == 0)
{
if (Physics.Raycast(ray.origin, ray.direction, out hit, 100))
{
inDirection = Vector3.Reflect(ray.direction, hit.normal);
ray = new Ray(hit.point, inDirection);
Debug.DrawRay(hit.point, inDirection * 100, Color.yellow);//์๋น์ ์ด๋ฏ๋ก ํ๋ ์ด์ด์ ํ๋ฉด์์๋ ๋ณด์ด์ง ์์
Debug.Log("Object name: " + hit.transform.name);//Debug์ด๋ฏ๋ก ํ๋ ์ด์ด์ ํ๋ฉด์์๋ ๋ณด์ด์ง ์์
if (hit.transform.name == "Lock")
{
Debug.Log("Start animation");
}
if (nReflections == 1)
{
lineRenderer.positionCount = ++nPoints;//positionCount๋ ์ ์ ๊ฐฏ์(๊ตฝ์ ๊ฐฏ์)
}
lineRenderer.SetPosition(i + 1, hit.point);
}
else
{
lineRenderer.SetPosition(i + 1, transform.forward * 5000);
}
}
else
{
if (Physics.Raycast(ray.origin, ray.direction, out hit, 100))//์ถฉ๋ ํ๋ค๋ฉด
{
inDirection = Vector3.Reflect(inDirection, hit.normal);
ray = new Ray(hit.point, inDirection);
Debug.DrawRay(hit.point, inDirection * 100, Color.yellow);//์๋น์
Debug.Log("Object name: " + hit.transform.name);
if (hit.transform.name == "Lock")
{
Debug.Log("Start animation");
}
lineRenderer.positionCount = ++nPoints;
lineRenderer.SetPosition(i + 1, hit.point);
}
else
{
lineRenderer.SetPosition(i + 1, transform.forward * 5000);
}
}
}
}
}```