注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

316Studio

分享资源,分享快乐··

 
 
 

日志

 
 

Unity中雷达效果的制作  

2010-04-27 10:39:17|  分类: Unity 3D |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

想给你游戏中增加个雷达么?Unity中可以很容易增加一个雷达.就像下面这样.当然.你可以让美术把雷达做的更好看一些.

Unity中雷达效果的制作 - 316studio - 316Studio

将下面的脚本放在一个游戏对象上,给"blip"设置一个纹理.相当于雷达的信号效果.选择一个纹理作为雷达的背景(radarBG),设置地图在屏幕上的位置,设置一个游戏对象用于你雷达的centerObject(通常是目前的玩家对象).确保所有的"敌人"的标签tag都是"Enemy".之后你就可以在你的游戏中看到雷达了.

两种语言任选其一.

Unity中雷达效果的制作 - 316studio - 316Studio js 代码

view source

print?

001

@script ExecuteInEditMode()

002

// radar! by PsychicParrot, adapted from a Blitz3d script found in the public domain online somewhere ..

003

//Modified by Dastardly Banana to add radar size configuration, different colors for enemies in different states (patrolling or chasing), ability to move radar to either one of 9 preset locations or to custom location.

004

//some lines are particular to our AI script, you will need to change "EnemyAINew" to the name of your AI script, and change "isChasing" to the boolean within that AI script that is true when the enemy is active/can see the player/is chasing the player.

005

var

blip : Texture;

// texture to use when the enemy isn't chasing

006

var

blipChasing : Texture;

//When Chasing

007

var

radarBG : Texture;

008

var

centerObject : Transform;

009

var

mapScale = 0.3;

010

var

mapSizePercent = 15;

011

var

checkAIscript : boolean =

true

;

012

var

enemyTag =

"Enemy"

;

013

enum radarLocationValues {topLeft, topCenter, topRight, middleLeft, middleCenter, middleRight, bottomLeft, bottomCenter, bottomRight, custom}

014

var

radarLocation : radarLocationValues = radarLocationValues.bottomLeft;

015

private

var

mapWidth : float;

016

private

var

mapHeight : float;

017

private

var

mapCenter : Vector2;

018

var

mapCenterCustom : Vector2;

019

function

Start () {

020

    

setMapLocation();  

021

}

022

function

OnGUI () {

023

//  GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3(Screen.width / 600.0, Screen.height / 450.0, 1));

024

    

// Draw player blip (centerObject)

025

    

bX=centerObject.transform.position.x * mapScale;

026

    

bY=centerObject.transform.position.z * mapScale;  

027

    

GUI.DrawTexture(Rect(mapCenter.x - mapWidth/2,mapCenter.y-mapHeight/2,mapWidth,mapHeight),radarBG);

028

    

 

029

    

// Draw blips for Enemies

030

    

DrawBlipsForEnemies();

031

    

 

032

}

033

  

 

034

function

drawBlip(go,aTexture){

035

    

 

036

    

centerPos=centerObject.position;

037

    

extPos=go.transform.position;

038

    

 

039

    

// first we need to get the distance of the enemy from the player

040

    

dist=Vector3.Distance(centerPos,extPos);

041

     

 

042

    

dx=centerPos.x-extPos.x;

// how far to the side of the player is the enemy?

043

    

dz=centerPos.z-extPos.z;

// how far in front or behind the player is the enemy?

044

    

 

045

    

// what's the angle to turn to face the enemy - compensating for the player's turning?

046

    

deltay=Mathf.Atan2(dx,dz)*Mathf.Rad2Deg - 270 - centerObject.eulerAngles.y;

047

    

 

048

    

// just basic trigonometry to find the point x,y (enemy's location) given the angle deltay

049

    

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

050

    

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

051

    

 

052

    

bX=bX*mapScale;

// scales down the x-coordinate so that the plot stays within our radar

053

    

bY=bY*mapScale;

// scales down the y-coordinate so that the plot stays within our radar

054

    

 

055

    

if

(dist<=mapWidth*.5/mapScale){

056

        

// this is the diameter of our largest radar circle

057

       

GUI.DrawTexture(Rect(mapCenter.x+bX,mapCenter.y+bY,4,4),aTexture);

058

 

 

059

    

}

060

 

 

061

}

062

 

 

063

function

DrawBlipsForEnemies(){

064

    

//You will need to replace isChasing with a variable from your AI script that is true when     the enemy is chasing the player, or doing watever you want it to be doing when it is red on    the radar.

065

    

 

066

    

//You will need to replace "EnemyAINew with the name of your AI script

067

    

 

068

    

// Find all game objects tagged Enemy

069

    

var

gos : GameObject[];

070

    

gos = GameObject.FindGameObjectsWithTag(enemyTag);

071

 

 

072

    

var

distance = Mathf.Infinity;

073

    

var

position = transform.position;

074

 

 

075

    

// Iterate through them and call drawBlip function

076

    

for

(

var

go : GameObject

in

gos)  {

077

          

var

blipChoice : Texture = blip;

078

          

if

(checkAIscript){

079

                

var

aiScript : EnemyAI = go.GetComponent(

"EnemyAI"

);

080

            

if

(aiScript.isChasing)

081

                    

blipChoice = blipChasing;

082

        

}

083

        

drawBlip(go,blipChoice);

084

    

}

085

 

 

086

}

087

function

setMapLocation () {

088

    

mapWidth = Screen.width*mapSizePercent/100.0;

089

    

mapHeight = mapWidth;

090

    

//sets mapCenter based on enum selection

091

    

if

(radarLocation == radarLocationValues.topLeft){

092

        

mapCenter = Vector2(mapWidth/2, mapHeight/2);

093

    

}

else

if

(radarLocation == radarLocationValues.topCenter){

094

        

mapCenter = Vector2(Screen.width/2, mapHeight/2);

095

    

}

else

if

(radarLocation == radarLocationValues.topRight){

096

        

mapCenter = Vector2(Screen.width-mapWidth/2, mapHeight/2);

097

    

}

else

if

(radarLocation == radarLocationValues.middleLeft){

098

        

mapCenter = Vector2(mapWidth/2, Screen.height/2);

099

    

}

else

if

(radarLocation == radarLocationValues.middleCenter){

100

        

mapCenter = Vector2(Screen.width/2, Screen.height/2);

101

    

}

else

if

(radarLocation == radarLocationValues.middleRight){

102

        

mapCenter = Vector2(Screen.width-mapWidth/2, Screen.height/2);

103

    

}

else

if

(radarLocation == radarLocationValues.bottomLeft){

104

        

mapCenter = Vector2(mapWidth/2, Screen.height - mapHeight/2);

105

    

}

else

if

(radarLocation == radarLocationValues.bottomCenter){

106

        

mapCenter = Vector2(Screen.width/2, Screen.height - mapHeight/2);

107

    

}

else

if

(radarLocation == radarLocationValues.bottomRight){

108

        

mapCenter = Vector2(Screen.width-mapWidth/2, Screen.height - mapHeight/2);

109

    

}

else

if

(radarLocation == radarLocationValues.custom){

110

        

mapCenter = mapCenterCustom;

111

    

}

112

    

 

113

}

Unity中雷达效果的制作 - 316studio - 316Studio csharp 代码

using UnityEngine;

using System.Collections;

public class Radar : MonoBehaviour

{

    public enum RadarTypes : int {Textured, Round, Transparent};

    public enum RadarLocations : int {TopLeft, TopCenter, TopRight, BottomLeft, BottomCenter, BottomRight, Left, Center, Right, Custom};

    

    // Display Location

    public RadarLocations radarLocation = RadarLocations.BottomCenter;

    public Vector2 radarLocationCustom;

    public RadarTypes radarType = RadarTypes.Round;

    public Color radarBackgroundA = new Color(255, 255, 0);

    public Color radarBackgroundB = new Color(0, 255, 255);

    public Texture2D radarTexture;

    public float radarSize = 0.20f;  // The amount of the screen the radar will use

    public float radarZoom = 0.60f;

    

    // Center Object information

    public bool   radarCenterActive;

    public Color  radarCenterColor = new Color(255, 255, 255);

    public string radarCenterTag;

    

    // Blip information

    public bool   radarBlip1Active;

    public Color  radarBlip1Color = new Color(0, 0, 255);

    public string radarBlip1Tag;

    public bool   radarBlip2Active;

    public Color  radarBlip2Color = new Color(0, 255, 0);

    public string radarBlip2Tag;

    public bool   radarBlip3Active;

    public Color  radarBlip3Color = new Color(255, 0, 0);

    public string radarBlip3Tag;

    public bool   radarBlip4Active;

    public Color  radarBlip4Color = new Color(255, 0, 255);

    public string radarBlip4Tag;

    

    // Internal vars

    private GameObject _centerObject;

    private int        _radarWidth;

    private int        _radarHeight;

    private Vector2    _radarCenter;

    private Texture2D  _radarCenterTexture;

    private Texture2D  _radarBlip1Texture;

    private Texture2D  _radarBlip2Texture;

    private Texture2D  _radarBlip3Texture;

    private Texture2D  _radarBlip4Texture;

    // Initialize the radar

    void Start ()

    {

        // Determine the size of the radar

        _radarWidth = (int)(Screen.width * radarSize);

        _radarHeight = _radarWidth;

        

        // Get the location of the radar

        setRadarLocation();

        // Create the blip textures

        _radarCenterTexture = new Texture2D(3, 3, TextureFormat.RGB24, false);

        _radarBlip1Texture = new Texture2D(3, 3, TextureFormat.RGB24, false);

        _radarBlip2Texture = new Texture2D(3, 3, TextureFormat.RGB24, false);

        _radarBlip3Texture = new Texture2D(3, 3, TextureFormat.RGB24, false);

        _radarBlip4Texture = new Texture2D(3, 3, TextureFormat.RGB24, false);

        

        CreateBlipTexture(_radarCenterTexture, radarCenterColor);

        CreateBlipTexture(_radarBlip1Texture, radarBlip1Color);

        CreateBlipTexture(_radarBlip2Texture, radarBlip2Color);

        CreateBlipTexture(_radarBlip3Texture, radarBlip3Color);

        CreateBlipTexture(_radarBlip4Texture, radarBlip4Color);

        

        // Setup the radar background texture

        if (radarType != RadarTypes.Textured)

        {

            radarTexture = new Texture2D(_radarWidth, _radarHeight, TextureFormat.RGB24, false);

            CreateRoundTexture(radarTexture, radarBackgroundA, radarBackgroundB);

        }

        

        // Get our center object

        GameObject[] gos;

        gos = GameObject.FindGameObjectsWithTag(radarCenterTag);

        _centerObject = gos[0];

    }

    

    // Update is called once per frame

    void OnGUI ()

    {

        GameObject[] gos;

        // Draw th radar background

        if (radarType != RadarTypes.Transparent)

        {

            Rect radarRect = new Rect(_radarCenter.x - _radarWidth / 2, _radarCenter.y - _radarHeight / 2, _radarWidth, _radarHeight);

            GUI.DrawTexture(radarRect, radarTexture);

        }

        // Draw blips

        if (radarBlip1Active)

        {

            // Find all game objects

            gos = GameObject.FindGameObjectsWithTag(radarBlip1Tag);

  

            // Iterate through them and call drawBlip function

            foreach (GameObject go in gos)

            {

                drawBlip(go, _radarBlip1Texture);

            }

        }

        if (radarBlip2Active)

        {

            gos = GameObject.FindGameObjectsWithTag(radarBlip2Tag);

  

            foreach (GameObject go in gos)

            {

                drawBlip(go, _radarBlip2Texture);

            }

        }

        if (radarBlip3Active)

        {

            gos = GameObject.FindGameObjectsWithTag(radarBlip3Tag);

  

            foreach (GameObject go in gos)

            {

                drawBlip(go, _radarBlip3Texture);

            }

        }

        if (radarBlip4Active)

        {

            gos = GameObject.FindGameObjectsWithTag(radarBlip4Tag);

  

            foreach (GameObject go in gos)

            {

                drawBlip(go, _radarBlip4Texture);

            }

        }

        // Draw center oject

        if (radarCenterActive)

        {

            Rect centerRect = new Rect(_radarCenter.x - 1.5f, _radarCenter.y - 1.5f, 3, 3);

            GUI.DrawTexture(centerRect, _radarCenterTexture);

        }

    }

    

    // Draw a blip for an object

    void drawBlip(GameObject go, Texture2D blipTexture)

    {

        if (_centerObject)

        {

            Vector3 centerPos = _centerObject.transform.position;

            Vector3 extPos = go.transform.position;

    

            // Get the distance to the object from the centerObject

            float dist = Vector3.Distance(centerPos, extPos);

            // Get the object's offset from the centerObject

            float bX = centerPos.x - extPos.x;

            float bY = centerPos.z - extPos.z;

    

            // Scale the objects position to fit within the radar

            bX = bX * radarZoom;

            bY = bY * radarZoom;

    

            // For a round radar, make sure we are within the circle

            if(dist <= (_radarWidth - 2) * 0.5 / radarZoom)

            {

                Rect clipRect = new Rect(_radarCenter.x - bX - 1.5f, _radarCenter.y + bY - 1.5f, 3, 3);

                GUI.DrawTexture(clipRect, blipTexture);

            }

        }

    }

    // Create the blip textures

    void CreateBlipTexture(Texture2D tex, Color c)

    {

        Color[] cols = {c, c, c, c, c, c, c, c, c};

        tex.SetPixels(cols, 0);

        tex.Apply();

    }

    

    // Create a round bullseye texture

    void CreateRoundTexture(Texture2D tex, Color a, Color b)

    {

        Color c = new Color(0, 0, 0);

        int size = (int)((_radarWidth / 2) / 4);

        

        // Clear the texture

        for (int x = 0; x < _radarWidth; x++)

        {

            for (int y = 0; y < _radarWidth; y++)

            {

                tex.SetPixel(x, y, c);

            }

        }

        

        for (int r = 4; r > 0; r--)

        {

            if (r % 2 == 0)

            {

                c = a;

            }

            else

            {

                c = b;

            }

            DrawFilledCircle(tex, (int)(_radarWidth / 2), (int)(_radarHeight / 2), (r * size), c);

        }

        

        tex.Apply();

    }

    

    // Draw a filled colored circle onto a texture

    void DrawFilledCircle(Texture2D tex, int cx, int cy, int r, Color c)

    {

        for (int x = -r; x < r ; x++)

        {

            int height = (int)Mathf.Sqrt(r * r - x * x);

            for (int y = -height; y < height; y++)

                tex.SetPixel(x + cx, y + cy, c);

        }

    }

    // Figure out where to put the radar

    void setRadarLocation()

    {

        // Sets radarCenter based on enum selection

        if(radarLocation == RadarLocations.TopLeft)

        {

            _radarCenter = new Vector2(_radarWidth / 2, _radarHeight / 2);

        }

        else if(radarLocation == RadarLocations.TopCenter)

        {

            _radarCenter = new Vector2(Screen.width / 2, _radarHeight / 2);

        }

        else if(radarLocation == RadarLocations.TopRight)

        {

            _radarCenter = new Vector2(Screen.width - _radarWidth / 2, _radarHeight / 2);

        }

        else if(radarLocation == RadarLocations.Left)

        {

            _radarCenter = new Vector2(_radarWidth / 2, Screen.height / 2);

        }

        else if(radarLocation == RadarLocations.Center)

        {

            _radarCenter = new Vector2(Screen.width / 2, Screen.height / 2);

        }

        else if(radarLocation == RadarLocations.Right)

        {

            _radarCenter = new Vector2(Screen.width - _radarWidth / 2, Screen.height / 2);

        }

        else if(radarLocation == RadarLocations.BottomLeft)

        {

            _radarCenter = new Vector2(_radarWidth / 2, Screen.height - _radarHeight / 2);

        }

        else if(radarLocation == RadarLocations.BottomCenter)

        {

            _radarCenter = new Vector2(Screen.width / 2, Screen.height - _radarHeight / 2);

        }

            else if(radarLocation == RadarLocations.BottomRight)

        {

            _radarCenter = new Vector2(Screen.width - _radarWidth / 2, Screen.height - _radarHeight / 2);

        }

            else if(radarLocation == RadarLocations.Custom)

        {

            _radarCenter = radarLocationCustom;

        }

    }

    

}

  评论这张
 
阅读(2642)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017