前回はPlayerの移動を実装した。
[どうぶつの森 自作する]— 2357 (@95s7k84695a) March 29, 2020
昨日の午後の進捗
・WASDの移動の実装
・装備(?) 系の切り替えUI作成
切り替えUIはアークタンジェントつかって、マウス座標から、角度を計算して、どの装備アイコンの方向にマウスがあるか求めてる
(リプ欄にスクリプト) pic.twitter.com/QEMq3cHN4Y
装備切り替えUIの作成
このUIの作成にはアークタンジェントを用いてマウスの位置ベクトルからマウスが初期位置から動いた角度を求めたアークタンジェントとは
・三角関数タンジェントの逆関数逆関数はプログラム的に言うと入力値と出力値を逆転させた関数である
本来タンジェントは
入力値:角度(ラジアン)
出力値:座標(位置ベクトル)
であるが今回は入力値(角度)の方を知りたいので、位置ベクトルを入れれば、角度か得られる関数出力値:座標(位置ベクトル)
つまり
入力値:座標(位置ベクトル)
出力値:角度(ラジアン)
である関数がアークタンジェントである。出力値:角度(ラジアン)
逆関数について、軽く
関数y=f(x)
において、yの値がさだまれはxの値がただ1つに定まる(関数f(x)が単射である)すなわちxがyの関数
x=g(x)
と表せる時、x=g(x)をものと関数y=f(x)の逆関数といい
y=f-1(x)
また、y=f(x)の逆関数y=f-1(x)はy=x について対象である
において、yの値がさだまれはxの値がただ1つに定まる(関数f(x)が単射である)すなわちxがyの関数
x=g(x)
と表せる時、x=g(x)をものと関数y=f(x)の逆関数といい
y=f-1(x)
f-1(x) はfインバースX と読む
と表すまた、y=f(x)の逆関数y=f-1(x)はy=x について対象である
詳しくは「高校数学の美しい物語」へ
実装
構成
Weapon List (weaponList.cs)
・マネージャーユーザからのマウスの角度を入手
→対応するiconのメソッドを実行させる
icon (waponList_icon.cs)
マネージャーより、メソッドが実行される。Selected Image をアクティブな状態にする
Weapon Imageのspritの名前を返す
Selected Image
色 #00BFFFiconに色をつけて、選択されていることをわかりやすくする。
Weapon Image
「装備の画像」をもつ。iconにより、どの「装備の画像」をもつか決められている。
次(未作成)
C#スクリプト
weaponList.cs
using System.Collections;
using System.Linq;
using UnityEngine;
using static UnityEngine.Mathf;
public class WeaponList : MonoBehaviour {
public WeaponList_icon[] icon_script = new WeaponList_icon[8];
Vector2 startMousePos, currentMousePos;
void Start () {
//nullチェック
if (icon_script.Count (x => x == null) != 0)
Debug.LogError ("Not be set Script in button of Weapon List.");
}
public void Active () { //このスクリプトのSetActiveをtrueにするとき一緒に呼ぶ
startMousePos = Input.mousePosition;
StartCoroutine (inputMouse ());
}
public void Sleep () { //このスクリプトのSetActiveをfalseにするとき一緒に呼ぶ
foreach (var i in icon_script) i.Sleep ();
}
int oldIndex;
IEnumerator inputMouse () {
oldIndex = 1; //初期値指定
while (true) {
currentMousePos = ((Vector2) Input.mousePosition) - startMousePos;
//どのUIが選択されているかを取得
int index = getIndexNum (currentMousePos);
if (oldIndex != index) { //違うものが選択されたら
icon_script[index].Active();
icon_script[oldIndex].Sleep();
Debug.Log(icon_script[index].WeaponName);
oldIndex = index;
}
yield return null;
}
}
//座標から、マウスがさすUIのインデックスを返す
private int getIndexNum (Vector2 Pos) {
var value = 8 * Atan2 (Pos.y, Pos.x);
if (Abs (value) > 7 * PI) return 2;
if (value < -5 * PI) return 3;
if (value < -3 * PI) return 4;
if (value < -2 * PI) return 5;
if (value < PI) return 6;
if (value < 3 * PI) return 7;
if (value < 5 * PI) return 0;
return 1;
}
}
waponList_icon.cs using UnityEngine;
using UnityEngine.UI;
public class WeaponList_icon : MonoBehaviour
{
public Image selectedImage;
public Image weaponImage;
public string WeaponName {
get { return weaponImage.sprite.name; }
}
private void Start() {
if(selectedImage == null) //nullチェック
Debug.LogError("Not be set Selected Image in Button of Weapon List.");
}
public void Active() { //このスクリプトのSetActiveをtrueにするとき一緒に呼ぶ
selectedImage.gameObject.SetActive(true);
}
public void Sleep() { //このスクリプトのSetActiveをfalseにするとき一緒に呼ぶ
selectedImage.gameObject.SetActive(false);
}
}


0 件のコメント:
コメントを投稿