Created
April 29, 2013 11:39
-
-
Save tsubaki/5481097 to your computer and use it in GitHub Desktop.
Unity3Dのサンプル。 targetに指定したオブジェクトのビューポート座標を取得し、アタッチしたオブジェクトの座標を変換するサンプル。主にGUITextやGUITextureを対象の上に配置したい場合に使用する。 これで名前を配置する場合、カメラが3Dビューであっても距離による拡縮が入らない。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[ExecuteInEditMode] | |
[RequireComponent(typeof(GUIText))] | |
public class FollowGUI : MonoBehaviour { | |
[SerializeField] | |
GameObject target; | |
[SerializeField] | |
Vector2 correctPos; | |
Camera _camera; | |
Vector3 correctPoint; | |
void Start() | |
{ | |
_camera = Camera.mainCamera; | |
correctPoint = Vector3.right * correctPos.x + Vector3.up * correctPos.y; | |
} | |
void Update () | |
{ | |
if( target == null) | |
return; | |
Vector3 viewportPoint = _camera.WorldToViewportPoint(target.transform.position ); | |
transform.position = correctPoint + viewportPoint; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment