TestMove
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestMove : MonoBehaviour//クラス名がファイル名と同一
{
public float speed = 3.0f;//publicにするとUnityの画面で値が弄れる。この場合は速さ
bool keyflag = false;//Keyが押された過去があるかどうか判定する変数
bool rotationflag = true;//一回だけ回転状態を取得するようの変数
Vector3 position;//現在の3次元位置ベクトルを保存する変数。これに足すことによって進む
Vector3 foword;//回転情報から前を割り出すためのベクトル
// Start is called before the first frame update
void Start()//一回だけ最初に呼ばれる関数。ここで初期化とかする
{
}
// Update is called once per frame
void Update()//Startの後に常に呼ばれる関数。モニターのフレームによって一秒間に呼ばれる回数が違う
{
if (Input.GetKeyDown(KeyCode.M)) keyflag=true;//Mキーが押されたら、フラグをオンにする
if (keyflag == true)//フラグがオンにされてたら
{
if (rotationflag == true)//それが最初だったら
{
transform.localPosition = new Vector3(0.2396085f, 0.8641855f, 1.249f);//初めの場所を良い感じの位置にする
position = transform.position;//それをポジションに入れる
foword=transform.rotation * new Vector3(0, 0, 1.0f);//回転情報と原点から真っすぐ前を向くベクトルをかけて今の状態の真っすぐを作る
rotationflag = false;//二回目はしないようにする
}
position+= foword*speed * Time.deltaTime;//あとは真っすぐの情報と速さ、フレームの間隔を掛けたものを足していく
transform.position=position;//それを現在の場所にする
}
}
}
TestCreate
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCreate : MonoBehaviour
{
[SerializeField] GameObject magic;//この宣言の仕方をすると自由にオブジェクトをここにアタッチ出来る
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.M))//Mキーが押されたら、
{
var parent = this.transform;//親の移動や回転の情報を入れておく
GameObject instance = Instantiate(magic, Vector3.zero, Quaternion.identity, parent);//それを使ってさっきアタッチしたオブジェクトを生成する。
}
}
}
CompleteMove
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CompleteMove : MonoBehaviour
{
public float speed = 3.0f;
public float deletetime = 3;//削除時間を決めれるようにpublicな変数として宣言する
bool rotationflag = true;
Vector3 foword;
Vector3 position;
float starttime;//最初の時間を入れる用
float time;.//現在時間を入れる用
// Start is called before the first frame update
void Start()
{
starttime = Time.time;//最初の時間を入れる
}
// Update is called once per frame
void Update()
{
time = Time.time;//現在時間を入れる
if (rotationflag == true)//作り出された一回目で
{
transform.localPosition = new Vector3(0.2396085f, 0.8641855f, 2f);
transform.rotation = transform.parent.rotation;//回転情報を入れておく
position = transform.position;
foword = transform.rotation * new Vector3(0, 0, 1.0f);
rotationflag = false;
}
position += foword * speed * Time.deltaTime;
transform.position = position;
if (time - starttime > deletetime) Destroy(this.gameObject);//現在時間と最初の時間の差を見て、削除時間になっていたら、削除する
}
}
コメント