找回密码
 立即注册

Unity3D简单的小地图代码

已有 2736 次阅读2012-12-1 09:24 | 地图

@script ExecuteInEditMode() 

public var blip : Texture; //定義一個指針文件代表角色 
public var radarBG : Texture; //地圖背景圖片,我直接用場景里我創建的render texture 

public var centerObject : Transform; //選擇角色的物體的位置信息 
public var mapScale = 0.3; //地圖縮放 
public var mapCenter = Vector2(50,50); //地圖中心 

function OnGUI () { 

bX=centerObject.transform.position.x * mapScale; 
bY=centerObject.transform.position.z * mapScale; 
   
bX=centerObject.transform.position.x * mapScale; 
bY=centerObject.transform.position.z * mapScale; 
   
GUI.DrawTexture(Rect(mapCenter.x-32,mapCenter.y-32,64,64),radarBG); 
// 上面的mapCenter.x-32是地圖的x位置,mapCenter.y-32是y位置,64,64是地圖的大小 

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 - 270 - centerObject.eulerAngles.y; 

bX=dist*Mathf.Cos(deltay * Mathf.Deg2Rad); 
bY=dist*Mathf.Sin(deltay * Mathf.Deg2Rad); 

bX=bX*mapScale; 
bY=bY*mapScale; 

if(dist<=200){ 

   GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y+bY,2,2),aTexture); 
function DrawBlipsForEnemies(){ 

var gos : GameObject&#91;&#93;; 
gos = GameObject.FindGameObjectsWithTag("Enemy"); 

var distance = Mathf.Infinity; 
var position = transform.position; 

for (var go : GameObject in gos)   { 

drawBlip(go,blip); 
Enemy应该加到Enemy标签下。 

      rendertexture 是可以被渲染到纹理。基于渲染效果,动态阴影,投射器,反射或检视相机。 
可以加到camera的target texture来显示相机的拍摄内容。 

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-17 18:52 , Processed in 0.063683 second(s), 15 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

返回顶部