プレーヤー③(プレーヤーの移動範囲を制限する)
![7b1085ce 733d 48c1 97d3 37ec103b8e40](https://codegenius.org/uploads/slide/image/100/7b1085ce-733d-48c1-97d3-37ec103b8e40.jpeg)
Mathf.Clamp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 0.2f;
// ★追加
private Vector3 pos;
void Update()
{
// Inputの前に「-」を付ける。
float moveH = -Input.GetAxis("Horizontal") * moveSpeed;
float moveV = -Input.GetAxis("Vertical") * moveSpeed;
transform.Translate(moveH, 0.0f, moveV);
// ★追加
Clamp();
}
// ★追加
// プレーヤーの移動できる範囲を制限する命令ブロック
void Clamp()
{
// プレーヤーの位置情報を「pos」という箱の中に入れる。
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
}
![C9a9d7a9 e1f1 4389 9e7a 756ffbdfe89d](https://codegenius.org/uploads/slide/image/101/c9a9d7a9-e1f1-4389-9e7a-756ffbdfe89d.jpeg)
Danmaku I(基礎1/全22回)
他のコースを見る![7b1085ce 733d 48c1 97d3 37ec103b8e40](https://codegenius.org/uploads/slide/image/100/7b1085ce-733d-48c1-97d3-37ec103b8e40.jpeg)
Mathf.Clamp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 0.2f;
// ★追加
private Vector3 pos;
void Update()
{
// Inputの前に「-」を付ける。
float moveH = -Input.GetAxis("Horizontal") * moveSpeed;
float moveV = -Input.GetAxis("Vertical") * moveSpeed;
transform.Translate(moveH, 0.0f, moveV);
// ★追加
Clamp();
}
// ★追加
// プレーヤーの移動できる範囲を制限する命令ブロック
void Clamp()
{
// プレーヤーの位置情報を「pos」という箱の中に入れる。
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
}
![C9a9d7a9 e1f1 4389 9e7a 756ffbdfe89d](https://codegenius.org/uploads/slide/image/101/c9a9d7a9-e1f1-4389-9e7a-756ffbdfe89d.jpeg)
プレーヤー③(プレーヤーの移動範囲を制限する)