[unity3d] 小地图

public var worldRefObj : Transform;//场景侦测参考物体;

public var worldPObj : Transform;//场景侦测物体;

public var mapBg : Texture2D;//地图背景;

public var mapPe : Texture2D;//指针背景;

private var mapPos : Vector2;

private var angle : float;//指针旋转

private var mapPeSize;//指针大小

function Update()
{
var worldPos = Vector2(worldPObj.transform.position.x - worldRefObj.transform.position.x,worldPObj.transform.position.z - worldRefObj.transform.position.z);

var worldRefObjSize = Vector2(worldRefObj.transform.collider.size.x,worldRefObj.transform.collider.size.z);

var mapBgSize = Vector2(mapBg.width,mapBg.height);

mapPeSize = Vector2(mapPe.width,mapPe.height);

angle = worldPObj.rotation.eulerAngles.y;

mapPos = Vector2(worldPos.x * mapBgSize.x/worldRefObjSize.x-mapPeSize.x/2, worldPos.y * mapBgSize.y/worldRefObjSize.y+mapPeSize.x/2);

//Debug.Log(mapPeSize);
}

function OnGUI()
{
GUI.DrawTexture(Rect(0,0,mapBg.width,mapBg.height),mapBg);

GUIUtility.RotateAroundPivot (angle, Vector2(mapPos.x+mapPeSize.x*0.5,-mapPos.y+mapPeSize.y*0.5));

Debug.Log(mapPeSize);

GUI.DrawTexture(Rect(mapPos.x,-mapPos.y,mapPe.width,mapPe.height),mapPe);
}

 

 

 

 

 

另外一个脚本代码

  1. public var blip : Texture; //代表角色
  2. public var blip_other : Texture;//敌人
  3. public var radarBG : Texture; //地图背景图片
  4. public var centerObject : Transform; //角色位置
  5. public var mapScale = 0.6; //地图缩放
  6. public var mapCenter = Vector2(50,50); //地图中心
  7. function OnGUI () {
  8.     bX=centerObject.position.x * mapScale;
  9.     bY=centerObject.position.z * mapScale;
  10.     
  11.     //bX=centerObject.transform.position.x * mapScale;
  12.     //bY=centerObject.transform.position.z * mapScale;
  13.     
  14.     GUI.DrawTexture(Rect(mapCenter.x -50,mapCenter.y - 50,100,100),radarBG);
  15.     GUI.DrawTexture(Rect(mapCenter.x,mapCenter.y,4,4),blip);
  16.    DrawBlipsForEnemies();
  17. }
  18. function DrawBlipsForCows(){
  19.   
  20.     var gos : GameObject[];
  21.     gos = GameObject.FindGameObjectsWithTag("Cow");
  22.     var distance = Mathf.Infinity;
  23.     var position = transform.position;
  24.     for (var go : GameObject in gos)  {
  25.         drawBlip(go,blip);
  26.     }
  27. }
  28. function drawBlip(go,aTexture){
  29.   
  30.     centerPos=centerObject.position;
  31.     extPos=go.transform.position;
  32.   
  33.     dist=Vector3.Distance(centerPos,extPos);
  34.     
  35.     dx=centerPos.x-extPos.x;
  36.     dz=centerPos.z-extPos.z;
  37.   
  38.     deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg + 90 - centerObject.eulerAngles.y;
  39.   
  40.     bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
  41.     bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
  42.   
  43.     bX=bX*mapScale*7;
  44.     bY=bY*mapScale*7;
  45.   
  46.     if(dist<=20){  //20以内的敌人显示
  47.        GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y+bY,4,4),aTexture);
  48.     }
  49. }
  50. function DrawBlipsForEnemies(){
  51.   
  52.     var gos : GameObject[];
  53.     gos = GameObject.FindGameObjectsWithTag("Enemy");
  54.     var distance = Mathf.Infinity;
  55.     var position = transform.position;
  56.     for (var go : GameObject in gos)  {
  57.     drawBlip(go,blip_other);
  58.     }
  59. }
posted @ 2012-03-14 12:52  渡蓝  阅读(616)  评论(0)    收藏  举报