Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

코딩inf

#4 에임 위에 있는 오브젝트 색 변화시키기(6/8) 본문

Unity/TowerDiffence

#4 에임 위에 있는 오브젝트 색 변화시키기(6/8)

D-coding 2021. 6. 8. 11:54

진짜 이거 찾는데만 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]

 

 

 

Camera_1.cs
0.01MB
Enemy.cs
0.00MB
Ray.cs
0.00MB

 

[파일]

 

새 폴더.zip
1.44MB