Unity虛擬搖桿的實(shí)現(xiàn)方法
更新時(shí)間:2020年04月15日 14:19:09 作者:yy763496668
這篇文章主要為大家詳細(xì)介紹了Unity虛擬搖桿的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)虛擬搖桿的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)置搖桿的背景圖片的錨點(diǎn)如下:

設(shè)置搖桿的錨點(diǎn)為背景圖片的中心點(diǎn)。
并給搖桿綁定腳本如下:
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System;
public class JoyStickController : MonoBehaviour,IDragHandler,IEndDragHandler {
//最大的拖動(dòng)距離
public float maxDragDistance = 50f;
//虛擬搖桿的方向
public Vector3 direction;
//玩家
public GameObject player;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//屏幕上的y軸分量 當(dāng)作游戲世界里的z分量
//設(shè)置玩家的朝向
player.transform.forward = new Vector3(direction.x,0,direction.y);
int flag = Vector3.Distance(Vector3.zero, this.transform.localPosition) <1f ? 0 : 1;
player.transform.Translate(Vector3.forward * flag * Time.deltaTime,Space.Self);
}
//拖拽中的時(shí)候
public void OnDrag(PointerEventData eventData)
{
this.transform.position = Input.mousePosition;
if (Vector3.Distance(Vector3.zero,this.transform.localPosition) > maxDragDistance)
{
direction = this.transform.position - Vector3.zero;
this.transform.localPosition = direction.normalized * maxDragDistance;
}
}
//拖拽結(jié)束的時(shí)候
public void OnEndDrag(PointerEventData eventData)
{
this.transform.localPosition = Vector3.zero;
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- unity實(shí)現(xiàn)手機(jī)端搖桿控制人物移動(dòng)
- Unity實(shí)現(xiàn)簡(jiǎn)單的虛擬搖桿
- unity實(shí)現(xiàn)虛擬搖桿控制Virtual Joystick
- Unity UGUI通過(guò)搖桿控制角色移動(dòng)
- unity實(shí)現(xiàn)手游虛擬搖桿
- Unity3D基于UGUI實(shí)現(xiàn)虛擬搖桿
- Unity3D使用UGUI開(kāi)發(fā)原生虛擬搖桿
- Unity實(shí)現(xiàn)簡(jiǎn)單虛擬搖桿
- Unity使用ScrollRect制作搖桿
- Unity實(shí)現(xiàn)簡(jiǎn)單搖桿的制作
相關(guān)文章
C#實(shí)現(xiàn)的xml操作類完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的xml操作類,包含C#針對(duì)xml的創(chuàng)建、刪除、遍歷、插入等常見(jiàn)操作,需要的朋友可以參考下2016-06-06
C#中計(jì)算時(shí)間差中的小數(shù)問(wèn)題解決
C#中計(jì)算時(shí)間差中的小數(shù)問(wèn)題解決需要的朋友可以參考一下2013-03-03

