Box Colider

2020. 5. 25. 11:34
728x90

>>> 3D

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
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UIElements;
 
public class Test : MonoBehaviour
{
    public UITest uiTest;
 
    public void Init()
    {
    }
 
    void Start()
    {
        this.Init();
        this.uiTest.Init();
    }
 
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
 
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 1000))
            {
                var target = hit.collider.gameObject.tag;
                Debug.LogFormat("target: {0}", target);
                if (target != null)
                {
                    this.uiTest.ShowThumb();
                }
                else 
                {
                    this.uiTest.HideThumb();
                }
            }
        }
    }
}
 
cs

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UITest : MonoBehaviour
{
    public GameObject thumbGO;
    public GameObject thumbEmptyGo;
 
    public void Init()
    {
        this.HideThumb();
    }
 
    public void ShowThumb()
    {
        this.thumbGO.SetActive(true);
        this.thumbEmptyGo.SetActive(false);
    }
 
    public void HideThumb()
    {
        this.thumbGO.SetActive(false);
        this.thumbEmptyGo.SetActive(true);
    }
}
 
cs

 

 

 

>>> 2D

using Box Colider 2D

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
using UnityEngine;
 
public class Test : MonoBehaviour
{
    public Hero hero;
 
    void Start()
    {
        
    }
 
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit2D = Physics2D.Raycast(ray.origin, ray.direction * 1000);
            if (hit2D)
            {
                Debug.LogFormat("target: {0}", hit2D.point);
                // this.hero.Move(hit2D.point);
            }
        }
    }
}
 
cs

 

728x90

'Unity > Study' 카테고리의 다른 글

DrawWireArc(Attack arange)  (0) 2020.05.25
DOTween(캐릭터 공격력 효과)  (0) 2020.05.25
Atlas  (0) 2020.05.21
Main Camer, UI Camera  (0) 2020.05.21
팝업창 open/close  (0) 2020.05.19

BELATED ARTICLES

more