アイテムの作成③(移動速度アップ)


移動速度アップ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// ★変更(privateに変更する)
// 初期の移動速度は遅くすること
private float moveSpeed = 0.1f;
private Vector3 pos;
void Update()
{
float moveH = Input.GetAxis("Horizontal") * moveSpeed;
float moveV = Input.GetAxis("Vertical") * moveSpeed;
transform.Translate(moveH, 0, moveV);
MoveClamp();
}
void MoveClamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
// ★追加
public void AddMoveSpeed(float amount)
{
moveSpeed += amount;
// 上限の設定
if(moveSpeed > 0.4f)
{
moveSpeed = 0.4f;
}
}
}

移動速度アップアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSpeedUp : ItemBase // クラス継承(ポイント)
{
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
ItemGet(); // スーパークラスで定義されたメソッドを使うことができる。
other.GetComponent<PlayerMovement>().AddMoveSpeed(0.1f);
}
}
}




【2021版】Danmaku(基礎/全55回)
他のコースを見る

移動速度アップ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// ★変更(privateに変更する)
// 初期の移動速度は遅くすること
private float moveSpeed = 0.1f;
private Vector3 pos;
void Update()
{
float moveH = Input.GetAxis("Horizontal") * moveSpeed;
float moveV = Input.GetAxis("Vertical") * moveSpeed;
transform.Translate(moveH, 0, moveV);
MoveClamp();
}
void MoveClamp()
{
pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -10, 10);
pos.z = Mathf.Clamp(pos.z, -10, 10);
transform.position = pos;
}
// ★追加
public void AddMoveSpeed(float amount)
{
moveSpeed += amount;
// 上限の設定
if(moveSpeed > 0.4f)
{
moveSpeed = 0.4f;
}
}
}

移動速度アップアイテム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSpeedUp : ItemBase // クラス継承(ポイント)
{
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
ItemGet(); // スーパークラスで定義されたメソッドを使うことができる。
other.GetComponent<PlayerMovement>().AddMoveSpeed(0.1f);
}
}
}




アイテムの作成③(移動速度アップ)