//tips
//Rigidbodyがアタッチされているオブジェクトの瞬間移動
調べていたらRigidbodyが付いたものをtransform.positionで移動させない方が良いということがわかった。
Rigidbodyの衝突計算は、Update前のタイミングで行われており、transform.positionで動かした場合、衝突計算を再計算して余計な負荷がかかるからのようです。
この場合、rigidbody.positionという機能が用意されているので、こちらを使用する。rigd.position = transform.position + transform.forward * 3.0f;で現在座標から三マス前方に瞬間移動する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFlash : MonoBehaviour
{
public float speed =3.0f; //プレイヤーの動くスピード
private Vector3 Player_pos; //プレイヤーのポジション
private Rigidbody rigd;
[SerializeField]
ReceiveEvent receiveEvent;
void Start()
{
Player_pos = GetComponent<Transform>().position; //最初の時点でのプレイヤーのポジションを取得
rigd = GetComponent<Rigidbody>(); //プレイヤーのRigidbodyを取得
}
void FixedUpdate()
{
if (receiveEvent.onflash)
{
transform.rotation = Quaternion.LookRotation(receiveEvent.directionfix);
//rigd.velocity = transform.forward * speed;
rigd.position = transform.position + transform.forward * 3.0f;
receiveEvent.onflash = false;
}
}
}
一度使用したボタンはクールタイムが発生する機能も追加する。MyDragExit()のタイミングで座標の向きを送る処理は終えているのでここに処理を追加する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReceiveEvent : MonoBehaviour
{
Vector3 originpos;
Vector3 movepos;
Vector3 heading;
Vector3 direction;
public Vector3 directionfix;
public bool onflash = false;
bool cooldown = false;
float countTime = 0;
private void Start()
{
Transform transform = GetComponent<RectTransform>();
originpos = transform.position;
}
void Update()
{
if (cooldown)
{
countTime += Time.deltaTime; //スタートしてからの秒数を格納
if(countTime >= 3.0f)
{
cooldown = false;
countTime = 0;
}
}
}
public void MyDragUI()//Drag
{
if (cooldown != true)
{
transform.position = Input.mousePosition;
}
}
public void MyDragExit()//pointerupへ
{
movepos = transform.position;
Dragsimulate();
transform.position = originpos;
cooldown = true;
}
public void Dragsimulate()
{
heading = movepos - originpos;
var distance = heading.magnitude;
direction = heading / distance;
Debug.Log(direction);
directionfix = new Vector3(direction.x,0,direction.y);//2dから3dへ変換
Debug.Log(directionfix);
onflash = true;
}
}
このように三秒のクールダウン時間を設けた。クール時間が分かりづらいのでImage TypeをFilledにして、Fill Methodで表示方法をわかりやすくしていく。
ImageにアタッチされているReceiveEventスクリプトにFill Methodを扱うコードを追加していく。
maxtimeをフィールドに追加し、GetComponent<Image>().fillAmount = countTime / maxtime;をUpdateメソッドに挿入する。filledの種類は円状に再生されるradial360にした。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ReceiveEvent : MonoBehaviour
{
Vector3 originpos;
Vector3 movepos;
Vector3 heading;
Vector3 direction;
public Vector3 directionfix;
public bool onflash = false;
bool cooldown = false;
float countTime = 0;
[SerializeField]
int maxtime = 3;
private void Start()
{
Transform transform = GetComponent<RectTransform>();
originpos = transform.position;
}
void Update()
{
if (cooldown)
{
countTime += Time.deltaTime; //スタートしてからの秒数を格納
GetComponent<Image>().fillAmount = countTime / maxtime;
if (countTime >= 3.0f)
{
cooldown = false;
countTime = 0;
}
}
}
public void MyDragUI()//Drag
{
if (cooldown != true)
{
transform.position = Input.mousePosition;
}
}
public void MyDragExit()//pointerupへ
{
movepos = transform.position;
Dragsimulate();
transform.position = originpos;
cooldown = true;
}
public void Dragsimulate()
{
heading = movepos - originpos;
var distance = heading.magnitude;
direction = heading / distance;
Debug.Log(direction);
directionfix = new Vector3(direction.x,0,direction.y);//2dから3dへ変換
Debug.Log(directionfix);
onflash = true;
}
}
次にUIボタンのpointerdownで3dオブジェクトを表示させ、UIの動きを反映させ、pointerupで操作した3dオブジェクトの場所にプレイヤーを移動させる機能を作る。
プレイヤーの子要素にカプセルを作成し、透過するマテリアルをアタッチしておく。また、異なる動作をさせるのでボタンも他のものを用意し、プレイヤーの動作スクリプトは新しく作成する。
ボタンのReceiveEventスクリプトにはimageを触った際に発動するスクリプトを追加しておく。public void MyDragIn()//pointerdownとして挿入した。新規動作スクリプトに連携させるため中身はまだ記載しない。imageのEventtriggerのpointerdownを設定しておく。
UI操作でGhostを移動させるのでDragsimulateの一時的な処理ではなく、updateメソッドを使用した継続的な座標の書き換えが必要なことがわかる。
混乱するのでReceiveEventスクリプトではなく新規スクリプトを作成することにする。