Unity :移動について、テストしてみたこと。Rigidbody.velocityとRandom.Range()を使って。
Unity 移動に関するテストのまとめ。
テスト①
れいさんのサイトからコピーしてきたスクリプトでまずやってみた。
これはキー操作で左右に動かすもの。Horizontal のキー操作のみ。
動画👇
テスト② C# IdoRigidVelo.cs
①にVerticalのキー操作を加えて上下にも移動するようにアレンジしたもの。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IdoRigidVelo : MonoBehaviour
{
public float speed;
//プライベート変数
//private Animator anim = null;
private Rigidbody2D rb = null;
void Start()
{
//コンポーネントのインスタンスを捕まえる
// anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
float xSpeed = 2.0f;
float ySpeed = 0.0f;
rb.velocity = new Vector2(xSpeed, ySpeed);
}
void Update()
{
//キー入力されたら行動する
float horizontalKey = Input.GetAxis("Horizontal");
float VerticalKey = Input.GetAxis("Vertical");
float xSpeed = 2.0f;
float ySpeed = 0.0f;
rb.velocity = new Vector2(xSpeed, ySpeed);
Debug.Log(horizontalKey);
if (horizontalKey > 0)
{
// Debug.Log(horizontalKey);
// transform.localScale = new Vector3(1, 1, 1);
// anim.SetBool("run", true);
xSpeed = speed;
}
else if (horizontalKey < 0)
{
// transform.localScale = new Vector3(-1, 1, 1);
//anim.SetBool("run", true);
xSpeed = -speed;
}
else
{
//anim.SetBool("run", false);
xSpeed = 0.0f;
}
if (VerticalKey > 0)
{
// transform.localScale = new Vector3(1, 1, 1);
// anim.SetBool("run", true);
ySpeed = speed;
}
else if (VerticalKey < 0)
{
// transform.localScale = new Vector3(-1, 1, 1);
//anim.SetBool("run", true);
ySpeed = -speed;
}
else
{
//anim.SetBool("run", false);
ySpeed = 0.0f;
}
rb.velocity = new Vector2(xSpeed, ySpeed);
}
動画👇
テスト③ C# IdoAutomatic キー操作によらず勝手に動くもの。
using System.Collections.Generic;
using UnityEngine;
public class IdoAutomatic : MonoBehaviour
{
private float xDirection = 1.0f;
private float yDirection = 0.0f;
void Update()
{
Rigidbody2D rb = this.transform.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(xDirection, yDirection);
}
}
動画👇
テスト④ C# IdoReverse
③に手を加え一定時間が経過すると動きが反転するようにしたもの。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IdoReverse : MonoBehaviour
{
private float chargeTime = 2.0f;
private float timeCount = 0.0f;
private float xDirection = 1.0f;
private float yDirection = 1.0f;
void Update()
{
Rigidbody2D rb = this.transform.GetComponent<Rigidbody2D>();
timeCount += Time.deltaTime;
rb.velocity = new Vector2(xDirection, yDirection);
// 指定時間の経過(条件)
if (timeCount > chargeTime)//タイムカウントが設定したチャージタイム(2秒)を超えたら
{
xDirection = -xDirection;
yDirection = -yDirection;
timeCount = 0;//タイムカウントをゼロに戻す
}
}
}
動画👇
テスト⑤
④に更に手を加え、上下左右2秒ごとに動く方向をランダムに変えるもの。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IdoRandom : MonoBehaviour
{
private float chargeTime = 2.0f;
private float timeCount = 0.0f;
private float xDirection = 1.0f;
private float yDirection = 0.0f;
void Update()
{
Rigidbody2D rb = this.transform.GetComponent<Rigidbody2D>();
timeCount += Time.deltaTime;
rb.velocity = new Vector2(xDirection, yDirection);
// 指定時間の経過(条件)
if (timeCount > chargeTime)//タイムカウントが設定したチャージタイム(2秒)を超えたら
{
xDirection = Random.Range(-1.0f, 1.0f);//x方向のためにランダムな少数-1.0~1.0を取得
yDirection = Random.Range(-1.0f, 1.0f);//y方向のためにランダムな少数-1.0~1.0を取得
timeCount = 0;//タイムカウントをゼロに戻す
}
}
}
動画👇
スクリプトは全部で5つ作ってそれを全部赤い2DオブジェクトSquareにアタッチ。
一旦全部チェックを外し、見たいものだけにチェックをいれて、それぞれのスクリプトがどんな動きを作り出すかテストしてみた。
はてなブログに動画を貼り付けるやり方(Streamable)も分かったので、今回は動画入りで記録ができて、それも良かった!エンベッドのタブでコピーしたものを、はてなブログのHTMLの中に埋め込んだら、ブログの中に動画が登場!
次はかわいいかおもじアイコンの出し方も研究したいな~。
今日は収穫があった!(こん)