unity實(shí)現(xiàn)虛擬搖桿控制VirtualJoystick的方法

這篇文章主要講解了unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),臺(tái)山企業(yè)網(wǎng)站建設(shè),臺(tái)山品牌網(wǎng)站建設(shè),網(wǎng)站定制,臺(tái)山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,臺(tái)山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick的方法

using UnityEngine;
using UnityEngine.UI;
 
public class TouchJoystick : MonoBehaviour
{
 
  public GameObject go;//需要通過虛擬搖桿控制的目標(biāo)物體
  public float moveSpeed = 3;//移動(dòng)速度
 
  public Image touchPoint;//搖桿軸對象
 
  private Vector3 OriginalPos_TP;//搖桿軸的初始位置
  private RectTransform rectTransform_TP;//搖桿軸的位置組件
 
  private float radius;//搖桿軸移動(dòng)的最大半徑
 
  void Start()
  {
    radius = this.GetComponent<RectTransform>().rect.width*0.5f;
    rectTransform_TP = touchPoint.GetComponent<RectTransform>();
    OriginalPos_TP = rectTransform_TP.position;
  }
 
  void Update()
  {
    //第一次觸摸屏幕時(shí),整個(gè)虛擬搖桿的位置更新
    if (Input.GetMouseButtonDown(0))
    {
      this.GetComponent<RectTransform>().position = Input.mousePosition;
      OriginalPos_TP = rectTransform_TP.position;
    }
 
    if (Input.GetMouseButton(0))
    {
      //取得觸摸點(diǎn)與虛擬軸初始點(diǎn)的距離
      float distance = Vector3.Distance(Input.mousePosition, OriginalPos_TP);
      //取得一個(gè)初始軸點(diǎn)指向觸摸點(diǎn)的向量
      Vector3 pos = Input.mousePosition - OriginalPos_TP;
      //如果距離大于可移動(dòng)半徑
      if (distance > radius)
        rectTransform_TP.position = OriginalPos_TP + pos.normalized*radius;//設(shè)置軸點(diǎn)到最大半徑位置
      else
        rectTransform_TP.position = Input.mousePosition;//否則軸點(diǎn)在當(dāng)前觸摸位置
      
      //以(0,1,0)為參考點(diǎn),計(jì)算單位軸向量與之夾角
      float angle = Vector3.Angle(new Vector3(0, 1, 0), new Vector3(pos.normalized.x, pos.normalized.y, 0));
      //移動(dòng)物體
      go.transform.Translate(new Vector3(0, 0, pos.normalized.magnitude*moveSpeed)*Time.deltaTime);
      //更新控制物體的旋轉(zhuǎn)與軸向方向一致
      if (pos.normalized.x > 0)
        go.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
      else
        go.transform.rotation = Quaternion.AngleAxis(-angle, Vector3.up);
    }
    else
      rectTransform_TP.position = OriginalPos_TP;//沒有觸摸時(shí)回到初始位置
  }
}

看完上述內(nèi)容,是不是對unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章標(biāo)題:unity實(shí)現(xiàn)虛擬搖桿控制VirtualJoystick的方法
網(wǎng)站網(wǎng)址:http://muchs.cn/article40/jepiho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站制作營銷型網(wǎng)站建設(shè)、虛擬主機(jī)網(wǎng)站內(nèi)鏈、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司