Unity Raycast - 2018-Interface-Programming-Exhibition/6team-Arduino_Game_Controller GitHub Wiki

ray

์‹œ์ž‘์ ๋ถ€ํ„ฐ ์ง€์ •ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ํ–ฅํ•˜๋Š” ๋ฌดํ•œ๋Œ€์˜ ์„ , ์ด๋ฅผ ์ด์šฉํ•ด ์›ํ•˜๋Š” ๋ฐฉํ–ฅ๊ณผ ๊ฑฐ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๋ฌผ์ฒด๋ฅผ ํ™•์ธํ•œ๋‹ค.

ํ•„์š”ํ•œ ๋ณ€์ˆ˜ : ๋ฐฉํ–ฅ, ์‹œ์ž‘์ 

raycast

physics์˜ ๋ฉ”์†Œ๋“œ๋กœ ray๊ฐ€ ์ถฉ๋Œ์ฒด์™€ ์ถฉ๋Œํ•˜๋ฉด True๋ฅผ ์•„๋‹ˆ๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฃผ๋กœ ์ถฉ๋Œ์ฒด์˜ ์ด๋ฆ„๊ณผ ์œ„์น˜๋ฅผ ์–ป๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

ํ•„์š”ํ•œ ๋ณ€์ˆ˜ : ์‹œ์ž‘์ , ๋ฐฉํ–ฅ, ์ถฉ๋Œ์ฒด(RaycastHit), ์ตœ๋Œ€๊ฑฐ๋ฆฌ, ๋ฌด์‹œํ•  ์ถฉ๋Œ์ฒด, queryTriggerInteraction (์œ„์˜ ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋‘ ํ•„์š”๋กœ ํ•˜์ง€๋Š” ์•Š์Œ)

DrawRay

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์€ ์„ ์„ ๊ทธ๋ฆฌ๋Š”๋ฐ ์“ฐ์ธ๋‹ค. ์ด๊ฒƒ ๋ง๊ณ ๋„ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ง€๋‚˜๊ฐ„ ๊ธธ์„ ์ž‡๋Š” ํŠธ๋ ˆ์ผ๋ Œ๋”๋Ÿฌ ๋“ฑ ๋งŽ์€ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค.

๋˜ lineRenderer.positionCount๋Š” ๋ผ์ธ๋ Œ๋”๋Ÿฌ๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด์ง„ ์„ ์˜ ์„ธ๊ทธ๋จผํŠธ(๊บฝ์—ฌ์ง€๋Š” ํšŸ์ˆ˜) ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค(๋˜ํ•œ ์ฝ์–ด์˜จ๋‹ค.)

RaycastHit

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);
                }
            }
        }
	}
}```
โš ๏ธ **GitHub.com Fallback** โš ๏ธ