まだまだ初見のメソッドが山ほど出てくるので、出てくるたびに全て吸収しなくてはですね。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
public GameObject gameManager;
public LayerMask blockLayer;
private Rigidbody2D rbody;
private const float MOVE_SPEED = 3;
private float moveSpeed;
private float jumpPower = 400;
private bool goJump = false;
private bool canJump = false;
private bool usingButtons = false;
public enum MOVE_DIR
{
STOP,
LEFT,
RIGHT,
};
private MOVE_DIR moveDirection = MOVE_DIR.STOP;
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
void Update()
{
canJump =
Physics2D.Linecast(transform.position - (transform.right * 0.3f),
transform.position - (transform.up * 0.1f), blockLayer) ||
Physics2D.Linecast(transform.position - (transform.right * 0.3f),
transform.position - (transform.up * 0.1f), blockLayer);
if (!usingButtons)
{
float x = Input.GetAxisRaw("Horizontal");
if (x == 0)
{
moveDirection = MOVE_DIR.STOP;
}
else
{
if (x < 0)
{
moveDirection = MOVE_DIR.LEFT;
}
else
{
moveDirection = MOVE_DIR.RIGHT;
}
}
if (Input.GetKeyDown("space"))
{
PushJumpButton();
}
}
}
// Update is called once per frame
void FixedUpdate()
{
switch (moveDirection)
{
case MOVE_DIR.STOP:
moveSpeed = 0;
break;
case MOVE_DIR.LEFT:
moveSpeed = MOVE_SPEED * -1;
transform.localScale = new Vector2(-1, 1);
break;
case MOVE_DIR.RIGHT:
moveSpeed = MOVE_SPEED;
transform.localScale = new Vector2(1, 1);
break;
}
rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);
if (goJump)
{
rbody.AddForce(Vector2.up * jumpPower);
goJump = false;
}
}
public void PushLeftButton()
{
moveDirection = MOVE_DIR.LEFT;
usingButtons = true;
}
public void PushRightButton()
{
moveDirection = MOVE_DIR.RIGHT;
usingButtons = true;
}
public void ReleaseMoveButton()
{
moveDirection = MOVE_DIR.STOP;
usingButtons = true;
}
public void PushJumpButton()
{
if (canJump)
{
goJump = true;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (gameManager.GetComponent<GameManager>().gameMode != GameManager.GAME_MODE.PLAY)
{
return;
}
if (col.gameObject.tag == "Trap")
{
gameManager.GetComponent<GameManager>().GameOver();
DestroyPlayer();
}
if (col.gameObject.tag == "Goal")
{
gameManager.GetComponent<GameManager>().GameClear();
}
}
void DestroyPlayer()
{
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject textGameOver;
public GameObject textClear;
public GameObject buttons;
public enum GAME_MODE
{
PLAY,
CLEAR,
};
public GAME_MODE gameMode = GAME_MODE.PLAY;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GameOver()
{
textGameOver.SetActive(true);
buttons.SetActive(false);
}
public void GameClear()
{
gameMode = GAME_MODE.CLEAR;
textClear.SetActive(true);
buttons.SetActive(false);
}
}