Notice
Recent Posts
Recent Comments
Link
«   2024/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
Archives
Today
Total
관리 메뉴

codingfarm

Ray 본문

Unity3d

Ray

scarecrow1992 2022. 2. 21. 00:37

 

https://docs.unity3d.com/kr/2018.4/Manual/CameraRays.html

 

화면상에 마우스가 존재하는 위치로 캐릭터를 돌아보게 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public Crosshairs crosshairs;
 
public void LookAt(Vector3 lookPoint) {
    Vector3 heightCorrectedPoint = new Vector3 (lookPoint.x, transform.position.y, lookPoint.z);
    transform.LookAt (heightCorrectedPoint);
}
 
void Update () {
    // Look input
    Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition);
    Plane groundPlane = new Plane (Vector3.up, Vector3.up * gunController.GunHeight);
    float rayDistance;
 
    if (groundPlane.Raycast(ray,out rayDistance)) {
        Vector3 point = ray.GetPoint(rayDistance);
        //Debug.DrawLine(ray.origin,point,Color.red);
        LookAt(point);
    }
}
cs

 

 

마우스를 클릭한 위치에 있는 오브젝트 얻기

1
2
3
4
5
6
7
8
RaycastHit hit;
 
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),
    out hit, 200.0f, LayerMask.GetMask("TileObject"))) {
 
    //Debug.Log(hit.point);
    GameObject object = hit.transform.gameObject;
}
cs

 

 

 

 

 

'Unity3d' 카테고리의 다른 글

시간  (0) 2022.02.21
생성  (0) 2022.02.21
이동  (0) 2022.02.21
코루틴  (0) 2022.02.20
5. 2d 플랫포머 - 내리막 길  (0) 2020.08.02
Comments