動くブロックの作り方


動くブロック
using UnityEngine;
public class MoveBlock : MonoBehaviour
{
private Vector3 pos;
public float speed;
private int num = -1;
void Update()
{
// 現在位置の取得
pos = transform.position;
// (テクニック)
// numの数字が「1」のときは「右方向」に移動
// numの数字が「-1」のときは「左方向」に移動
transform.Translate(new Vector3(1, 0, 0) * Time.deltaTime * speed * num);
// ここの条件の意味を考えること
if (pos.x > 15)
{
num = -1;
}
if (pos.x < 6)
{
num = 1;
}
}
}



【Unity6版】BallGame(全27回)
他のコースを見る

動くブロック
using UnityEngine;
public class MoveBlock : MonoBehaviour
{
private Vector3 pos;
public float speed;
private int num = -1;
void Update()
{
// 現在位置の取得
pos = transform.position;
// (テクニック)
// numの数字が「1」のときは「右方向」に移動
// numの数字が「-1」のときは「左方向」に移動
transform.Translate(new Vector3(1, 0, 0) * Time.deltaTime * speed * num);
// ここの条件の意味を考えること
if (pos.x > 15)
{
num = -1;
}
if (pos.x < 6)
{
num = 1;
}
}
}



動くブロックの作り方