資源簡介
【Unity】UGUI超級簡單的搖桿制作,搖桿控制物體移動!!!!!!!!!!!!!
代碼片段和文件信息
using?System;
using?System.Collections;
using?System.Collections.Generic;
using?UnityEngine;
using?UnityEngine.Events;
using?UnityEngine.EventSystems;
///?
///?time:2018/12/2
///?author:King.Sun
///?description:搖桿控制器
///?
public?class?JoyStick?:?MonoBehaviour
{
///?
///?搖桿背景
///?
private?Transform?_joyBg;
///?
///?遙感中心
///?
private?Transform?_joyCenter;
///?
///?搖桿半徑
///?
private?float?_radius;
///?
///?移動中心
///?
private?Vector2?_moveCenter;
///?
///?鼠標到終點的向量
///?
private?Vector2?_mouseToCenterVect;
///?
///?鼠標到中心點的距離
///?
private?float?_mouseToCenterDistance;
///?
///?水平獲取值
///?
private?float?_hor;
///?
///?垂直獲取值
///?
private?float?_ver;
///?
///?旋轉角度
///?
private?float?_rotAngle;
///?
///?主角
///?
private?Transform?_player;
///?
///?目標朝向
///?
private?Vector3?_forwardTarget;
//?Use?this?for?initialization
void?Start?()
{
_joyBg?=?Gameobject.Find(“Canvas“).transform.Find(“JoyBg“);
_joyCenter?=?Gameobject.Find(“Canvas“).transform.Find(“JoyBg/JoyCenter“);
_player?=?Gameobject.Find(“Player“).transform;
_radius?=?100;
}
private?void?Update()
{
//只有在水平或者垂直值大于0的情況下,主角才移動或旋轉
if?(Math.Abs(_hor)?>?0||Math.Abs(_ver)?>?0)
{
//Mathf.Clamp(_mouseToCenterDistance/10001):根據搖桿中心的移動距離太判斷速度
//new?Vector3(000.1f*Mathf.Clamp(_mouseToCenterDistance/10001)):在Z軸方向上移動Mathf.Clamp(_mouseToCenterDistance/10001)的距離
//不停地在Z軸方向上移動Mathf.Clamp(_mouseToCenterDistance/10001)的距離
_player.position?+=?_player.TransformDirection(new?Vector3(000.1f*Mathf.Clamp(_mouseToCenterDistance/10001)));
//根據搖桿的旋轉設置主角的朝向
_player.forward?=?_forwardTarget;
}
}
///?
///?開始拖動
///?
public?void?ondragBegain()
{
//移動中心點賦值
_moveCenter?=?Input.mousePosition;
//顯示搖桿
_joyBg.gameobject.SetActive(true);
//搖桿背景位置修正到點擊位置
_joyBg.position?=?_moveCenter;
//搖桿中心位置修正到點擊位置
_joyCenter.position?=?_moveCenter;
}
///?
///?正在拖動
///?
public?void?ondragMove()
{
//中心店到觸摸點的向量賦值
_mouseToCenterVect?=?(Vector2)Input.mousePosition?-?_moveCenter;
//中心店到觸摸點的距離計算
_mouseToCenterDistance?=?_mouseToCenterVect.magnitude;
//根據距離來判斷搖桿中心的位置
if?(_mouseToCenterDistance? {
//若是距離小于最大半徑,這里取向量的歸一值,就是模為1的向量,乘上中心到觸摸點的距離,這個就是搖桿中心應該移動的方向和距離,并且移動是在移動中心的基礎上,所以加上移動中心的坐標
_joyCenter.position?=?_mouseToCenterVect.normalized?*?_mouseToCenterDistance?+?_moveCenter;
}
else
{
//同上,不過就是限定了移動的最大距離
_joyCenter.position?=?_mouseToCenterVect.normalized?*?_radius?+?_moveCenter;
}
//搖桿中心的X?-?移動中心的x就是水平的變化值,這里?/100?控制_hor在(-1,1)之間
_hor?=?(_joyCenter.position.x?-?_moveCenter.x)/100;
//搖桿中心的Y?-?移動中心的Y就是垂直的變化值,
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-12-01?23:35??JoyStick\
?????目錄???????????0??2018-12-01?15:13??JoyStick\.idea\
?????目錄???????????0??2018-12-01?15:17??JoyStick\.idea\.idea.JoyStic\
?????目錄???????????0??2018-12-02?01:57??JoyStick\.idea\.idea.JoyStic\.idea\
?????文件????????2407??2018-12-01?21:57??JoyStick\.idea\.idea.JoyStic\.idea\contentModel.xm
?????文件?????????210??2018-12-01?15:17??JoyStick\.idea\.idea.JoyStic\.idea\indexLayout.xm
?????文件?????????309??2018-12-01?15:17??JoyStick\.idea\.idea.JoyStic\.idea\modules.xm
?????文件???????21891??2018-12-02?01:57??JoyStick\.idea\.idea.JoyStic\.idea\workspace.xm
?????文件?????????256??2018-12-01?15:17??JoyStick\.idea\.idea.JoyStic\riderModule.iml
?????文件???????20647??2018-12-01?23:35??JoyStick\Assembly-CSharp.csproj
?????目錄???????????0??2018-12-02?01:59??JoyStick\Assets\
?????文件????????2026??2018-12-01?21:54??JoyStick\Assets\Head.mat
?????文件?????????188??2018-12-01?20:26??JoyStick\Assets\Head.mat.me
?????文件????????4659??2018-12-02?01:57??JoyStick\Assets\JoyStick.cs
?????文件?????????243??2018-12-01?15:12??JoyStick\Assets\JoyStick.cs.me
?????文件????????2022??2018-12-01?21:54??JoyStick\Assets\Mat.mat
?????文件?????????188??2018-12-01?20:20??JoyStick\Assets\Mat.mat.me
?????目錄???????????0??2018-12-01?15:31??JoyStick\Assets\Plugins\
?????目錄???????????0??2018-12-01?15:31??JoyStick\Assets\Plugins\Editor\
?????目錄???????????0??2018-12-01?15:31??JoyStick\Assets\Plugins\Editor\JetBrains\
?????文件?????1342464??2018-09-13?16:34??JoyStick\Assets\Plugins\Editor\JetBrains\JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll
?????文件?????????569??2018-12-01?15:31??JoyStick\Assets\Plugins\Editor\JetBrains\JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll.me
?????文件?????????172??2018-12-01?15:31??JoyStick\Assets\Plugins\Editor\JetBrains.me
?????文件?????????172??2018-12-01?15:31??JoyStick\Assets\Plugins\Editor.me
?????文件?????????172??2018-12-01?15:31??JoyStick\Assets\Plugins.me
?????目錄???????????0??2018-12-02?01:57??JoyStick\Assets\Scenes\
?????文件???????26877??2018-12-02?01:57??JoyStick\Assets\Scenes\SampleScene.unity
?????文件?????????155??2018-07-25?02:00??JoyStick\Assets\Scenes\SampleScene.unity.me
?????文件?????????172??2018-07-25?02:00??JoyStick\Assets\Scenes.me
?????文件?????????172??2018-12-01?21:54??JoyStick\Assets\UnityChan.me
?????文件???????28799??2017-10-10?20:37??JoyStick\Assets\搖桿底板.png
............此處省略1838個文件信息
評論
共有 條評論