Teachers open the door but You must enter by yourself.
【事前学習】前回学んだ基本操作を再確認しておきましょう。
打ち返しパッドを追加してボールを操作できるようにしてみましょう。本ページの操作をただ読んだままに実践するだけでなく、それぞれの操作の意味を噛みしめながら進めてください。
//Pad.csに追加
void Start()
{
name="Pad";
transform.position=new Vector3(0,0,-3.5f);
transform.localScale=new Vector3(1,1,0.25f);
GetComponent<Renderer>().material.color=new Color(0,0,1,1);
}
using UnityEngine;
using UnityEngine.InputSystem;
public class Pad : MonoBehaviour
{
public float speed = 0.1f;
float dx;
public InputAction move;
void Start()
{
}
void Update()
{
}
void OnMove(InputAction.CallbackContext c)
{
Debug.Log("key pressed");
var vector2d = c.ReadValue<Vector2>();
dx = vector2d.x;
}
void FixedUpdate(){
var position = transform.position;
if (dx > 0 && position.x < 2){
//右方向キーが押された かつ ボールが枠内にある
position.x += speed; //パッドを右にずらす
}else if (dx < 0 && position.x > -2){
//左方向キーが押された かつ ボールが枠内にある
position.x -= speed; //パッドを左にずらす
}
transform.position = position;
}
void Awake()
{
move.performed+=OnMove;
}
void OnEnable()
{
move.Enable();
}