Unity制作小地圖和方向?qū)Ш?/h1>
      來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次
      
        
									
一、unity方向?qū)Ш街谱?/strong>
設(shè)計要求是方向?qū)Ш诫S著鼠標旋轉(zhuǎn)轉(zhuǎn)換方向,效果圖如下:
具體的實現(xiàn)方法主要有兩個步驟,分別為UI設(shè)計和腳本編寫。我的設(shè)計思路是這個控件分為兩層,第一層為東西南北指示層,第二層為圖標指示層,這里我的圖標采用圓形圖標,方向指示這里采用控制圖標旋轉(zhuǎn)的方式實現(xiàn),層級關(guān)系如下:
首先創(chuàng)建父節(jié)點1,然后在父節(jié)點下創(chuàng)建子節(jié)點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)){
  //按住鼠標左鍵才能調(diào)節(jié)角度,根據(jù)鼠標移動的快慢(增量), 獲得相機左右旋轉(zhuǎn)的角度(處理X) 
  float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; 
  //根據(jù)鼠標移動的快慢(增量), 獲得相機上下旋轉(zhuǎn)的角度(處理Y) 
  rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; 
  //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value 
  rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); 
  //總體設(shè)置一下相機角度 
  transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
   UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX);
  }
 }
}
二、unity小地圖的制作
關(guān)于小地圖的制作,網(wǎng)上各種帖子鋪天蓋地,然而仔細看卻發(fā)現(xiàn)大部分都一樣,互相抄襲,很多都是沒用的。各種帖子大都采用是正交相機的方式顯示小地圖,然而這個地圖是真實場景的俯視,我們需要的往往是像英雄聯(lián)盟那樣的小地圖,這里我采用一種簡單的方式實現(xiàn)小地圖。廢話不說先上效果圖:
這里的地圖只是一張圖片,這增加了地圖的靈活性,這里的小地圖創(chuàng)建跟上面方向?qū)Ш筋愃?,所不同的是腳本的編寫方式。
具體的實現(xiàn)也是分為兩個步驟,分別為UI的設(shè)計和代碼的編寫。
第一步:地圖UI的設(shè)計
層級關(guān)系如圖:
父節(jié)點1為背景地圖,子節(jié)點2為藍色箭頭,藍色箭頭表示目標目前所在的位置。這兩個節(jié)點僅僅是圖片控件。
第二步:腳本的編寫
腳本如下:
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鍵前進
  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鍵移動
  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ù)鼠標移動的快慢(增量), 獲得相機左右旋轉(zhuǎn)的角度(處理X) 
  float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; 
  //根據(jù)鼠標移動的快慢(增量), 獲得相機上下旋轉(zhuǎn)的角度(處理Y) 
  rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; 
  //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value 
  rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); 
  //總體設(shè)置一下相機角度 
  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í)有所幫助,也希望大家多多支持我們。
									
        
      
      
    
一、unity方向?qū)Ш街谱?/strong>
設(shè)計要求是方向?qū)Ш诫S著鼠標旋轉(zhuǎn)轉(zhuǎn)換方向,效果圖如下:
具體的實現(xiàn)方法主要有兩個步驟,分別為UI設(shè)計和腳本編寫。我的設(shè)計思路是這個控件分為兩層,第一層為東西南北指示層,第二層為圖標指示層,這里我的圖標采用圓形圖標,方向指示這里采用控制圖標旋轉(zhuǎn)的方式實現(xiàn),層級關(guān)系如下:
首先創(chuàng)建父節(jié)點1,然后在父節(jié)點下創(chuàng)建子節(jié)點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)){
  //按住鼠標左鍵才能調(diào)節(jié)角度,根據(jù)鼠標移動的快慢(增量), 獲得相機左右旋轉(zhuǎn)的角度(處理X) 
  float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; 
  //根據(jù)鼠標移動的快慢(增量), 獲得相機上下旋轉(zhuǎn)的角度(處理Y) 
  rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; 
  //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value 
  rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); 
  //總體設(shè)置一下相機角度 
  transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
   UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX);
  }
 }
}
二、unity小地圖的制作
關(guān)于小地圖的制作,網(wǎng)上各種帖子鋪天蓋地,然而仔細看卻發(fā)現(xiàn)大部分都一樣,互相抄襲,很多都是沒用的。各種帖子大都采用是正交相機的方式顯示小地圖,然而這個地圖是真實場景的俯視,我們需要的往往是像英雄聯(lián)盟那樣的小地圖,這里我采用一種簡單的方式實現(xiàn)小地圖。廢話不說先上效果圖:
這里的地圖只是一張圖片,這增加了地圖的靈活性,這里的小地圖創(chuàng)建跟上面方向?qū)Ш筋愃?,所不同的是腳本的編寫方式。
具體的實現(xiàn)也是分為兩個步驟,分別為UI的設(shè)計和代碼的編寫。
第一步:地圖UI的設(shè)計
層級關(guān)系如圖:
父節(jié)點1為背景地圖,子節(jié)點2為藍色箭頭,藍色箭頭表示目標目前所在的位置。這兩個節(jié)點僅僅是圖片控件。
第二步:腳本的編寫
腳本如下:
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鍵前進
  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鍵移動
  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ù)鼠標移動的快慢(增量), 獲得相機左右旋轉(zhuǎn)的角度(處理X) 
  float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX; 
  //根據(jù)鼠標移動的快慢(增量), 獲得相機上下旋轉(zhuǎn)的角度(處理Y) 
  rotationY += Input.GetAxis ("Mouse Y") * sensitivityY; 
  //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value 
  rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); 
  //總體設(shè)置一下相機角度 
  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í)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4890.html
您可能感興趣的文章
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
 - 01-10C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方
 - 01-10C#中DataGridView常用操作實例小結(jié)
 - 01-10c# ArrayList的使用方法小總結(jié)
 - 01-10C#一個簡單的定時小程序?qū)崿F(xiàn)代碼
 - 01-10C#中Winform獲取文件路徑的方法實例小結(jié)
 - 01-10Unity3d獲取系統(tǒng)時間
 - 01-10Unity3D獲取當(dāng)前鍵盤按鍵及Unity3D鼠標、鍵盤的基本操作
 - 01-10C#制作簡單的多人在線即時交流聊天室
 - 01-10C#微信開發(fā)(服務(wù)器配置)
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實現(xiàn)txt定位指定行完整實例
 - 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 04-02jquery與jsp,用jquery
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10delphi制作wav文件的方法
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 01-10C#中split用法實例總結(jié)
 


