코딩inf
#4 에임 위에 있는 오브젝트 색 변화시키기(6/8) 본문
진짜 이거 찾는데만 2시간 걸렸는데 알고보니 매우 간단했네요;
아무튼 코드는 아래와 같습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Ray : MonoBehaviour
{
private RaycastHit hit;
public TextMeshProUGUI look;
public GameObject[] before;
public Material[] MA;
void Start()
{
hit.collider.GetComponent<Renderer>();
}
void Update()
{
Raycast();
meshrenderer();
}
void Raycast()
{
if (Physics.Raycast(this.transform.position, transform.forward, out hit))
{
Debug.Log("hit point : " + hit.point + ", distance : " + hit.distance + ", name : " + hit.collider.name+ ", tag : " +hit.collider.tag);
look.text = ("hit point : " + hit.point + ", distance: " + hit.distance + ", name: " + hit.collider.name);
Debug.DrawRay(this.transform.position, transform.forward * 100000f, Color.red);
//hit.collider.gameObject.GetComponent<Renderer>().material.color = Color.red;
}
else {
Debug.DrawRay(this.transform.position, transform.forward * 100000f, Color.red);
}
}
void meshrenderer()
{
int i = 0;
for(i = 0; i < 33; i++)
{
if (hit.collider.name == before[i].name) { hit.collider.GetComponent<Renderer>().material = MA[1]; } else { before[i].GetComponent<Renderer>().material = MA[0]; }
}
}
}
전에 만들었던 Ray코드를 수정했습니다
새로운 변수와 함수를 만들어주고
for문 안에 코드를 먼저 볼께요
hit.collider.name은 에임위에 있는 오브젝트입니다.
그리고before[~~]는 게임 오브젝트이고 .name을 붙여서 에임위에 있는 오브젝트의 이름과
현제 게임상에 있는 어떤 오브젝트의 이름이 같으면
그 오브젝트의 material을 MA 로 바꾸어라 입니다.
하지만 그전에 MA에 바꾸고 싶은 material을 먼저 넣어주고 실행을 해야 합니다.
--------------------------------------------------------------------------------------------------
중간 점검
[오브젝트]
[Materials]
[Scripts]
[파일]
'Unity > TowerDiffence' 카테고리의 다른 글
#5 적 이동 스크립트 구현 / 타워설치(돈 감소) (6/24) (0) | 2021.06.24 |
---|---|
#3 에임위에 있는 오브젝트 이름 출력 [Raycast] (6/6) (0) | 2021.06.06 |
#2 입력키 출력--------------------------[6/5] (0) | 2021.06.05 |
#0 기획하기 (0) | 2021.06.04 |
#1 오브젝트 세팅 및 카메라 이동 --------------------[6/3] (0) | 2021.06.03 |