[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);
}
另外一个脚本代码
- public var blip : Texture; //代表角色
- public var blip_other : Texture;//敌人
- public var radarBG : Texture; //地图背景图片
- public var centerObject : Transform; //角色位置
- public var mapScale = 0.6; //地图缩放
- public var mapCenter = Vector2(50,50); //地图中心
- function OnGUI () {
- bX=centerObject.position.x * mapScale;
- bY=centerObject.position.z * mapScale;
- //bX=centerObject.transform.position.x * mapScale;
- //bY=centerObject.transform.position.z * mapScale;
- GUI.DrawTexture(Rect(mapCenter.x -50,mapCenter.y - 50,100,100),radarBG);
- GUI.DrawTexture(Rect(mapCenter.x,mapCenter.y,4,4),blip);
- DrawBlipsForEnemies();
- }
- function DrawBlipsForCows(){
- var gos : GameObject[];
- gos = GameObject.FindGameObjectsWithTag("Cow");
- var distance = Mathf.Infinity;
- var position = transform.position;
- for (var go : GameObject in gos) {
- drawBlip(go,blip);
- }
- }
- function drawBlip(go,aTexture){
- centerPos=centerObject.position;
- extPos=go.transform.position;
- dist=Vector3.Distance(centerPos,extPos);
- dx=centerPos.x-extPos.x;
- dz=centerPos.z-extPos.z;
- deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg + 90 - centerObject.eulerAngles.y;
- bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad);
- bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad);
- bX=bX*mapScale*7;
- bY=bY*mapScale*7;
- if(dist<=20){ //20以内的敌人显示
- GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y+bY,4,4),aTexture);
- }
- }
- function DrawBlipsForEnemies(){
- var gos : GameObject[];
- gos = GameObject.FindGameObjectsWithTag("Enemy");
- var distance = Mathf.Infinity;
- var position = transform.position;
- for (var go : GameObject in gos) {
- drawBlip(go,blip_other);
- }
- }
浙公网安备 33010602011771号