1. GameObject WorldObject;
  2. //this is the ui element
  3. RectTransform UI_Element;
  4. //first you need the RectTransform component of your canvas
  5. RectTransform CanvasRect=Canvas.GetComponent<RectTransform>();
  6. //then you calculate the position of the UI element
  7. //0,0 for the canvas is at the center of the screen, whereas WorldToViewPortPoint treats the lower left corner as 0,0. Because of this, you need to subtract the height / width of the canvas * 0.5 to get the correct position.
  8. Vector2 ViewportPosition=Camara.main.WorldToViewportPoint(WorldObject.transform.position);
  9. Vector2 WorldObject_ScreenPosition=new Vector2(
  10. ((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5f)),
  11. ((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5f)));
  12. //now you can set the position of the ui element
  13. UI_Element.anchoredPosition=WorldObject_ScreenPosition;



- 출처 : https://answers.unity.com/questions/799616/unity-46-beta-19-how-to-convert-from-world-space-t.html

유니티 프로젝트 폴더를 깃에 올리다 보면 .obj 모델 메쉬파일이 깃에 안올라와 있는 것을 알 수 있다

대개 이유는 자신의 gitignore에 *.obj 를 추가 했거나 global ignore list에 *.obj가 올라와 있는 경우이다.

위 두 경우 때문에 obj 확장자의 파일들이 커밋에 추가 되지 않았던 것이다


이를 해결하기 위해서는 해당 깃의 gitignore에 !Assets/**/*.obj 를 추가함으로 해결 할 수 있다

그럼 Assets 폴더 안에 모든 *.obj를 찾아내어서 그 파일들을 전부 허용해줄 것이다




유니티 Raycast를 사용할 때 특정한 오브젝트만 Raycast에 Target되기를 원하는 경우 LayerMask를 사용한다

하지만 LayerMask를 사용할 때 주의해야할 점이 


RaycastHit hitInfo;

Ray ray = Camera.main.ScreenPointToRay(InputManager.GetInputPos());



1.   if (Physics.Raycast(ray, out hitInfo, 1 << LayerMask.NameToLayer("특정 레이어") ))


2.   if (Physics.Raycast(ray, out hitInfo, float.MaxValue, 1 << LayerMask.NameToLayer("특정 레이어") ))



위의 1번과 2번의 차이는 중간에 float.MaxValue 즉 Raycast의 파라미터 중 maxDistance를 넣어주느냐 마냐의 차이이다


1번의 경우에는 Raycast는 1 << LayerMask.NameToLayer("특정 레이어") 를 flaot maxDistance 파라미터에 넣어주는 것으로 인식하여서 LayerMask가 제대로 적용되지 않는다. 함수가 프로그래머가 의도한 바와는 다르게 1 << LayerMask.NameToLayer("특정 레이어") 를 다른 parameter 값으로 인식한 경우다. 게다가 Raycast에는 maxDistance 파라미터가 없이 LayerMask를 파라미터로 넣는 함수형은 없다.


그래서 프로그래머는 maxDistance에 float.MaxValue를 넣어주어야만 뒤의 1 << LayerMask.NameToLayer("특정 레이어")를 프로그래머가 의도한대로 LayerMask의 값으로 인식한다

+ Recent posts