Unity制作小地圖和方向?qū)Ш?/h1>
更新時(shí)間:2020年05月28日 15:14:05 作者:u010989951
這篇文章主要為大家詳細(xì)介紹了Unity制作小地圖和方向?qū)Ш降姆椒ǎ闹惺纠a介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
一、unity方向?qū)Ш街谱?/strong>
設(shè)計(jì)要求是方向?qū)Ш诫S著鼠標(biāo)旋轉(zhuǎn)轉(zhuǎn)換方向,效果圖如下:


具體的實(shí)現(xiàn)方法主要有兩個(gè)步驟,分別為UI設(shè)計(jì)和腳本編寫。我的設(shè)計(jì)思路是這個(gè)控件分為兩層,第一層為東西南北指示層,第二層為圖標(biāo)指示層,這里我的圖標(biāo)采用圓形圖標(biāo),方向指示這里采用控制圖標(biāo)旋轉(zhuǎn)的方式實(shí)現(xiàn),層級關(guān)系如下:

首先創(chuàng)建父節(jié)點(diǎn)1,然后在父節(jié)點(diǎn)下創(chuàng)建子節(jié)點(diǎn)2,3;最后調(diào)整好位置。
第二步腳本編寫,腳本如下:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class shijiao : MonoBehaviour
{
public GameObject Gcanvas;
public GameObject UIzhinanpicture;
public GameObject Terren;
public GameObject SMAP;
//public GameObject bnt=GameObject.Find("Button");
//方向靈敏度
public float sensitivityX = 10F;
public float sensitivityY = 10F;
//上下最大視角(Y視角)
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
static public bool ifcanvas;
void Update()
{
if(Input.GetMouseButton (0)){
//按住鼠標(biāo)左鍵才能調(diào)節(jié)角度,根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)左右旋轉(zhuǎn)的角度(處理X)
float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
//根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)上下旋轉(zhuǎn)的角度(處理Y)
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
//角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
//總體設(shè)置一下相機(jī)角度
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX);
}
}
}
二、unity小地圖的制作
關(guān)于小地圖的制作,網(wǎng)上各種帖子鋪天蓋地,然而仔細(xì)看卻發(fā)現(xiàn)大部分都一樣,互相抄襲,很多都是沒用的。各種帖子大都采用是正交相機(jī)的方式顯示小地圖,然而這個(gè)地圖是真實(shí)場景的俯視,我們需要的往往是像英雄聯(lián)盟那樣的小地圖,這里我采用一種簡單的方式實(shí)現(xiàn)小地圖。廢話不說先上效果圖:


這里的地圖只是一張圖片,這增加了地圖的靈活性,這里的小地圖創(chuàng)建跟上面方向?qū)Ш筋愃疲煌氖悄_本的編寫方式。
具體的實(shí)現(xiàn)也是分為兩個(gè)步驟,分別為UI的設(shè)計(jì)和代碼的編寫。
第一步:地圖UI的設(shè)計(jì)
層級關(guān)系如圖:

父節(jié)點(diǎn)1為背景地圖,子節(jié)點(diǎn)2為藍(lán)色箭頭,藍(lán)色箭頭表示目標(biāo)目前所在的位置。這兩個(gè)節(jié)點(diǎn)僅僅是圖片控件。
第二步:腳本的編寫
腳本如下:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using UnityEngine.EventSystems;
public class shijiao : MonoBehaviour
{
public GameObject Gcanvas;//畫布
public GameObject UIzhinanpicture;
public GameObject Terren;//大地
public GameObject SMAP;//小地圖指針
public GameObject SMAPBK;//小地圖背景
GameObject Cm;
//方向靈敏度
public float sensitivityX = 10F;
public float sensitivityY = 10F;
//上下最大視角(Y視角)
public float minimumY = -60F;
public float maximumY = 60F;
//山地的大小
float Twidth;
float Tlongth;
//地圖大小
float mapwidth;
float maplongth;
//比例大小
static public float widthScale;
static public float longthscal;
//圖片縮放比例
//比例大小
//static public float PwidthScale;
//static public float Plongthscal;
float rotationY = 0F;
static public bool ifcanvas;
private float movespeed = 20;
CharacterController ctrlor;
void Start ()
{
RenderSettings.fog = false;
ifcanvas =true;
Gcanvas.SetActive (ifcanvas);
Cm = GameObject.Find("Mcam");
ctrlor = GetComponent<CharacterController>();
Twidth=Terren.GetComponent<Collider>().bounds.size.x;
Tlongth =Terren.GetComponent<Collider>().bounds.size.z;
mapwidth = SMAPBK.GetComponent<RectTransform>().rect.width;
maplongth = SMAPBK.GetComponent<RectTransform>().rect.height;
widthScale =(mapwidth) /Twidth;
longthscal =(maplongth) /Tlongth;
SMAP.transform.localPosition= new Vector3(Cm.transform.position.x* widthScale- 50, Cm.transform.position.z* longthscal-50,0);
}
void Update()
{
if (Input.GetMouseButton (1)) {
ifcanvas = true;
Gcanvas.SetActive (ifcanvas);
}
else{
if (Input.GetKey(KeyCode.Escape))
{
ifcanvas = false;
Gcanvas.SetActive (ifcanvas);
}
if (!EventSystem.current.IsPointerOverGameObject())
{
//W鍵前進(jìn)
if (Input.GetKey (KeyCode.W)) {
Vector3 forward = transform.TransformDirection(Vector3.forward);
ctrlor.Move(forward*movespeed*Time.deltaTime);
}
//S鍵后退
if (Input.GetKey(KeyCode.S))
{
Vector3 back = transform.TransformDirection(Vector3.back);
ctrlor.Move(back * movespeed * Time.deltaTime);
}
//A鍵移動(dòng)
if (Input.GetKey(KeyCode.A))
{
Vector3 left = transform.TransformDirection(Vector3.left);
ctrlor.Move(left* movespeed * Time.deltaTime);
}
//D鍵后退
if (Input.GetKey(KeyCode.D) && gameObject.transform.position.y > 0)
{
Vector3 right = transform.TransformDirection(Vector3.right);
ctrlor.Move(right * movespeed * Time.deltaTime);
}
//E鍵升高
if (Input.GetKey (KeyCode.E)) {
Vector3 upward = transform.TransformDirection(Vector3.up);
ctrlor.Move(upward * movespeed * Time.deltaTime);
}
SMAP.transform.localPosition = new Vector3(Cm.transform.position.x * widthScale - 50, Cm.transform.position.z * longthscal - 50, 0);
if (Input.GetMouseButton (0)){
//根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)左右旋轉(zhuǎn)的角度(處理X)
float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
//根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)上下旋轉(zhuǎn)的角度(處理Y)
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
//角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
//總體設(shè)置一下相機(jī)角度
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX);
SMAP.transform.localEulerAngles = new Vector3(0, 0, -rotationX);
}
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
VB.NET中TextBox的智能感知應(yīng)用實(shí)例
這篇文章主要介紹了VB.NET中TextBox的智能感知應(yīng)用實(shí)例,非常實(shí)用的功能,需要的朋友可以參考下 2014-08-08
-
c#橋接模式(bridge結(jié)構(gòu)模式)用法實(shí)例
這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細(xì)的分析了橋接模式的原理與用法實(shí)例,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下 2014-12-12
-
C#?DataSet結(jié)合FlyTreeView實(shí)現(xiàn)顯示樹狀模型數(shù)據(jù)
NineRays.WebControls.FlyTreeView?是?9rays.net?推出的一款功能強(qiáng)大的樹狀模型數(shù)據(jù)顯示控件,本文主要介紹了如何使用其并結(jié)合?DataSet對象進(jìn)行數(shù)據(jù)顯示,感興趣的可以了解下 2024-04-04
-
WPF實(shí)現(xiàn)html中的table控件的示例代碼
相信很多做WPF開發(fā)的小伙伴都遇到過表格類的需求,雖然現(xiàn)有的Grid控件也能實(shí)現(xiàn),但是使用起來的體驗(yàn)感并不好,所以本文我們就來用WPF自己實(shí)現(xiàn)一個(gè)html中的table控件吧 2024-03-03
-
Unity3D實(shí)現(xiàn)模型隨機(jī)切割
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)模型隨機(jī)切割,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2021-03-03
-
本文主要介紹了C#中利用GDI來繪制圖形和文字的方法,并提供的簡單的示例供大家參考學(xué)習(xí),希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-03-03
-
C#多態(tài)的三種實(shí)現(xiàn)方式(小結(jié))
這篇文章主要介紹了C#多態(tài)的三種實(shí)現(xiàn)方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧 2021-03-03
-
Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2022-11-11
最新評論
一、unity方向?qū)Ш街谱?/strong>
設(shè)計(jì)要求是方向?qū)Ш诫S著鼠標(biāo)旋轉(zhuǎn)轉(zhuǎn)換方向,效果圖如下:
具體的實(shí)現(xiàn)方法主要有兩個(gè)步驟,分別為UI設(shè)計(jì)和腳本編寫。我的設(shè)計(jì)思路是這個(gè)控件分為兩層,第一層為東西南北指示層,第二層為圖標(biāo)指示層,這里我的圖標(biāo)采用圓形圖標(biāo),方向指示這里采用控制圖標(biāo)旋轉(zhuǎn)的方式實(shí)現(xiàn),層級關(guān)系如下:
首先創(chuàng)建父節(jié)點(diǎn)1,然后在父節(jié)點(diǎn)下創(chuàng)建子節(jié)點(diǎn)2,3;最后調(diào)整好位置。
第二步腳本編寫,腳本如下:
using UnityEngine; using System.Collections; using UnityEngine.UI; public class shijiao : MonoBehaviour { public GameObject Gcanvas; public GameObject UIzhinanpicture; public GameObject Terren; public GameObject SMAP; //public GameObject bnt=GameObject.Find("Button"); //方向靈敏度 public float sensitivityX = 10F; public float sensitivityY = 10F; //上下最大視角(Y視角) public float minimumY = -60F; public float maximumY = 60F; float rotationY = 0F; static public bool ifcanvas; void Update() { if(Input.GetMouseButton (0)){ //按住鼠標(biāo)左鍵才能調(diào)節(jié)角度,根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)左右旋轉(zhuǎn)的角度(處理X) float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; //根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)上下旋轉(zhuǎn)的角度(處理Y) rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); //總體設(shè)置一下相機(jī)角度 transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0); UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX); } } }
二、unity小地圖的制作
關(guān)于小地圖的制作,網(wǎng)上各種帖子鋪天蓋地,然而仔細(xì)看卻發(fā)現(xiàn)大部分都一樣,互相抄襲,很多都是沒用的。各種帖子大都采用是正交相機(jī)的方式顯示小地圖,然而這個(gè)地圖是真實(shí)場景的俯視,我們需要的往往是像英雄聯(lián)盟那樣的小地圖,這里我采用一種簡單的方式實(shí)現(xiàn)小地圖。廢話不說先上效果圖:
這里的地圖只是一張圖片,這增加了地圖的靈活性,這里的小地圖創(chuàng)建跟上面方向?qū)Ш筋愃疲煌氖悄_本的編寫方式。
具體的實(shí)現(xiàn)也是分為兩個(gè)步驟,分別為UI的設(shè)計(jì)和代碼的編寫。
第一步:地圖UI的設(shè)計(jì)
層級關(guān)系如圖:
父節(jié)點(diǎn)1為背景地圖,子節(jié)點(diǎn)2為藍(lán)色箭頭,藍(lán)色箭頭表示目標(biāo)目前所在的位置。這兩個(gè)節(jié)點(diǎn)僅僅是圖片控件。
第二步:腳本的編寫
腳本如下:
using UnityEngine; using System.Collections; using UnityEngine.UI; using System; using UnityEngine.EventSystems; public class shijiao : MonoBehaviour { public GameObject Gcanvas;//畫布 public GameObject UIzhinanpicture; public GameObject Terren;//大地 public GameObject SMAP;//小地圖指針 public GameObject SMAPBK;//小地圖背景 GameObject Cm; //方向靈敏度 public float sensitivityX = 10F; public float sensitivityY = 10F; //上下最大視角(Y視角) public float minimumY = -60F; public float maximumY = 60F; //山地的大小 float Twidth; float Tlongth; //地圖大小 float mapwidth; float maplongth; //比例大小 static public float widthScale; static public float longthscal; //圖片縮放比例 //比例大小 //static public float PwidthScale; //static public float Plongthscal; float rotationY = 0F; static public bool ifcanvas; private float movespeed = 20; CharacterController ctrlor; void Start () { RenderSettings.fog = false; ifcanvas =true; Gcanvas.SetActive (ifcanvas); Cm = GameObject.Find("Mcam"); ctrlor = GetComponent<CharacterController>(); Twidth=Terren.GetComponent<Collider>().bounds.size.x; Tlongth =Terren.GetComponent<Collider>().bounds.size.z; mapwidth = SMAPBK.GetComponent<RectTransform>().rect.width; maplongth = SMAPBK.GetComponent<RectTransform>().rect.height; widthScale =(mapwidth) /Twidth; longthscal =(maplongth) /Tlongth; SMAP.transform.localPosition= new Vector3(Cm.transform.position.x* widthScale- 50, Cm.transform.position.z* longthscal-50,0); } void Update() { if (Input.GetMouseButton (1)) { ifcanvas = true; Gcanvas.SetActive (ifcanvas); } else{ if (Input.GetKey(KeyCode.Escape)) { ifcanvas = false; Gcanvas.SetActive (ifcanvas); } if (!EventSystem.current.IsPointerOverGameObject()) { //W鍵前進(jìn) if (Input.GetKey (KeyCode.W)) { Vector3 forward = transform.TransformDirection(Vector3.forward); ctrlor.Move(forward*movespeed*Time.deltaTime); } //S鍵后退 if (Input.GetKey(KeyCode.S)) { Vector3 back = transform.TransformDirection(Vector3.back); ctrlor.Move(back * movespeed * Time.deltaTime); } //A鍵移動(dòng) if (Input.GetKey(KeyCode.A)) { Vector3 left = transform.TransformDirection(Vector3.left); ctrlor.Move(left* movespeed * Time.deltaTime); } //D鍵后退 if (Input.GetKey(KeyCode.D) && gameObject.transform.position.y > 0) { Vector3 right = transform.TransformDirection(Vector3.right); ctrlor.Move(right * movespeed * Time.deltaTime); } //E鍵升高 if (Input.GetKey (KeyCode.E)) { Vector3 upward = transform.TransformDirection(Vector3.up); ctrlor.Move(upward * movespeed * Time.deltaTime); } SMAP.transform.localPosition = new Vector3(Cm.transform.position.x * widthScale - 50, Cm.transform.position.z * longthscal - 50, 0); if (Input.GetMouseButton (0)){ //根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)左右旋轉(zhuǎn)的角度(處理X) float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; //根據(jù)鼠標(biāo)移動(dòng)的快慢(增量), 獲得相機(jī)上下旋轉(zhuǎn)的角度(處理Y) rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); //總體設(shè)置一下相機(jī)角度 transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0); UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX); SMAP.transform.localEulerAngles = new Vector3(0, 0, -rotationX); } } } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VB.NET中TextBox的智能感知應(yīng)用實(shí)例
這篇文章主要介紹了VB.NET中TextBox的智能感知應(yīng)用實(shí)例,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08c#橋接模式(bridge結(jié)構(gòu)模式)用法實(shí)例
這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細(xì)的分析了橋接模式的原理與用法實(shí)例,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12C#?DataSet結(jié)合FlyTreeView實(shí)現(xiàn)顯示樹狀模型數(shù)據(jù)
NineRays.WebControls.FlyTreeView?是?9rays.net?推出的一款功能強(qiáng)大的樹狀模型數(shù)據(jù)顯示控件,本文主要介紹了如何使用其并結(jié)合?DataSet對象進(jìn)行數(shù)據(jù)顯示,感興趣的可以了解下2024-04-04WPF實(shí)現(xiàn)html中的table控件的示例代碼
相信很多做WPF開發(fā)的小伙伴都遇到過表格類的需求,雖然現(xiàn)有的Grid控件也能實(shí)現(xiàn),但是使用起來的體驗(yàn)感并不好,所以本文我們就來用WPF自己實(shí)現(xiàn)一個(gè)html中的table控件吧2024-03-03Unity3D實(shí)現(xiàn)模型隨機(jī)切割
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)模型隨機(jī)切割,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03- 本文主要介紹了C#中利用GDI來繪制圖形和文字的方法,并提供的簡單的示例供大家參考學(xué)習(xí),希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2016-03-03
C#多態(tài)的三種實(shí)現(xiàn)方式(小結(jié))
這篇文章主要介紹了C#多態(tài)的三種實(shí)現(xiàn)方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解
這篇文章主要為大家介紹了Unity輸出帶點(diǎn)擊跳轉(zhuǎn)功能的Log實(shí)現(xiàn)技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11最新評論