メソッドの2Dの抜け漏れやprefab設定後のタブの未設定などの細かいミスで全体のプログラムが動かなくなるので、慎重に作成していきます。
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();
}
if (col.gameObject.tag == "Enemy")
{
if (transform.position.y > col.gameObject.transform.position.y + 0.4f)
{
rbody.velocity = new Vector2(rbody.velocity.x, 0);
rbody.AddForce(Vector2.up * jumpPower);
col.gameObject.GetComponent<EnemyManager>().DestroyEnemy();
}
else
{
gameManager.GetComponent<GameManager>().GameOver();
DestroyPlayer();
}
}
if (col.gameObject.tag == "Orb")
{
col.gameObject.GetComponent<OrbManager>().GetOrb();
}
}
void DestroyPlayer()
{
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
private const int MAX_SCORE = 999999;
public GameObject textGameOver;
public GameObject textClear;
public GameObject buttons;
public GameObject textScoreNumber;
public enum GAME_MODE
{
PLAY,
CLEAR,
};
public GAME_MODE gameMode = GAME_MODE.PLAY;
private int score = 0;
private int displayScore = 0;
// Start is called before the first frame update
void Start()
{
RefreshScore();
}
// Update is called once per frame
void Update()
{
if(score > displayScore)
{
displayScore += 10;
if(displayScore > score)
{
displayScore = score;
}
RefreshScore();
}
}
public void GameOver()
{
textGameOver.SetActive(true);
buttons.SetActive(false);
}
public void GameClear()
{
gameMode = GAME_MODE.CLEAR;
textClear.SetActive(true);
buttons.SetActive(false);
}
public void AddScore(int val)
{
score += val;
if(score > MAX_SCORE)
{
score = MAX_SCORE;
}
}
void RefreshScore()
{
textScoreNumber.GetComponent<Text>().text = displayScore.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public LayerMask blockLayer;
private Rigidbody2D rbody;
private float moveSpeed = 1;
public enum MOVE_DIR
{
LEFT,
RIGHT,
};
private MOVE_DIR moveDirection = MOVE_DIR.LEFT;
// Start is called before the first frame update
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
bool isBlock;
switch (moveDirection)
{
case MOVE_DIR.LEFT:
rbody.velocity = new Vector2(moveSpeed * -1, rbody.velocity.y);
transform.localScale = new Vector2(1, 1);
isBlock = Physics2D.Linecast(
new Vector2(transform.position.x, transform.position.y + 0.5f),
new Vector2(transform.position.x - 0.3f, transform.position.y + 0.5f),
blockLayer);
if (isBlock)
{
moveDirection = MOVE_DIR.RIGHT;
}
break;
case MOVE_DIR.RIGHT:
rbody.velocity = new Vector2(moveSpeed, rbody.velocity.y);
transform.localScale = new Vector2(-1, 1);
isBlock = Physics2D.Linecast(
new Vector2(transform.position.x, transform.position.y + 0.5f),
new Vector2(transform.position.x + 0.3f, transform.position.y + 0.5f),
blockLayer);
if (isBlock)
{
moveDirection = MOVE_DIR.LEFT;
}
break;
}
}
public void DestroyEnemy()
{
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbManager : MonoBehaviour
{
private const int ORB_POINT = 100;
private GameObject gameManager;
// Start is called before the first frame update
void Start()
{
gameManager = GameObject.Find("GameManager");
}
// Update is called once per frame
void Update()
{
}
public void GetOrb()
{
gameManager.GetComponent<GameManager>().AddScore(ORB_POINT);
Destroy(this.gameObject);
}
}