RogueLike 2D Tutorial Help
Hello, I'm a beginner and i can't find my mistake(s) in these severals codes. I'm follow the lead of the tutorial but my player object doesn't want to move. Thanks for helping me <3
// Enemy code
using UnityEngine;
using System.Collections;
public class Enemy : MovingObject {
public int playerDamage;
private Animator animator;
private Transform target;
private bool skipMove;
protected override void Start ()
{
BordManager.instance.AddEnemyToList(this);
animator = GetComponent<Animator>();
target = GameObject.FindGameObjectWithTag("Player").transform;
base.Start();
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
if (skipMove)
{
skipMove = false;
return;
}
base.AttemptMove<T>(xDir, yDir);
skipMove = true;
}
public void MoveEnemy()
{
int xDir = 0;
int yDir = 0;
if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon)
{
yDir = target.position.y > transform.position.y ? 1 : -1;
} else {
xDir = target.position.x > transform.position.x ? 1 : -1;
}
AttemptMove<Player>(xDir, yDir);
}
protected override void CantMove<T>(T hit)
{
Player hitPlayer = hit as Player;
animator.SetTrigger("enemyAttack");
hitPlayer.LoseFood(playerDamage);
}
}
// Player code
using UnityEngine.SceneManagement;
using System.Collections;
using UnityEngine;
public class Player : MovingObject {
public int wallDamage = 1;
public int pointsPerFood = 10;
public int pointsPerSoda = 20;
public float restartLevelDelay = 1f;
private Animator animator;
private int food;
protected override void Start()
{
animator = GetComponent<Animator>();
food = BordManager.instance.playerFood;
base.Start();
}
private void OnDisable()
{
BordManager.instance.playerFood = food;
}
protected override void AttemptMove<T>(int xDir, int yDir)
{
food--;
base.AttemptMove<T>(xDir, yDir);
RaycastHit2D hit;
CheckIfGameOver();
BordManager.instance.playerTurn = false;
}
private void CheckIfGameOver()
{
if (food <= 0)
{
BordManager.instance.GameOver();
}
}
void Udapte()
{
if (!BordManager.instance.playerTurn) return;
int horizontal = 0;
int vertical = 0;
horizontal = (int)(Input.GetAxisRaw("Horizontal"));
vertical = (int)(Input.GetAxisRaw("Vertical"));
if (horizontal != 0)
{
vertical = 0;
}
if (horizontal != 0 || vertical != 0)
{
AttemptMove<Wall>(horizontal, vertical);
}
}
protected override void CantMove<T>(T hit)
{
Wall hitWall = hit as Wall;
hitWall.DamageWall(wallDamage);
animator.SetTrigger("playerChop");
}
public void Restart()
{
SceneManager.LoadScene(0);
}
public void LoseFood(int loss)
{
animator.SetTrigger("playerGetHit");
food -= loss;
CheckIfGameOver();
}
private void OnTriggerEnter2D(Collider2D other)
{
switch (other.tag)
{
case "Exit":
Invoke("Restart", restartLevelDelay);
enabled = false;
break;
case "Food":
food += pointsPerFood;
other.gameObject.SetActive(false);
break;
case "Soda":
food += pointsPerSoda;
other.gameObject.SetActive(false);
break;
}
}
}
// MovingObject code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class MovingObject : MonoBehaviour {
public BoxCollider2D boxCollider;
public Rigidbody2D rigbod2d;
public LayerMask blockingLayer;
public float moveTime = 0.2f;
private float inverseMoveTime;
protected virtual void Start ()
{
rigbod2d = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
inverseMoveTime = 1f / moveTime;
}
protected bool CanWeMove(int xDir, int yDir, out RaycastHit2D hit)
{
Vector2 currentPosition = transform.position;
Vector2 end = currentPosition + new Vector2(xDir, yDir);
boxCollider.enabled = false;
hit = Physics2D.Linecast(currentPosition, end, blockingLayer);
boxCollider.enabled = true;
if (hit.transform == null)
{
StartCoroutine(SmoothMovement(end));
return true;
}
return false;
}
protected IEnumerator SmoothMovement(Vector3 finalPosition)
{
float wayToEnd = (transform.position - finalPosition).sqrMagnitude;
while (wayToEnd > float.Epsilon)
{
Vector3 newPosition = Vector3.MoveTowards(rigbod2d.position, finalPosition, inverseMoveTime * Time.deltaTime);
rigbod2d.MovePosition(newPosition);
wayToEnd = (transform.position - finalPosition).sqrMagnitude;
yield return null;
}
}
protected virtual void AttemptMove<T>(int xDir, int yDir)
where T : Component
{
RaycastHit2D hit;
bool movement = CanWeMove(xDir, yDir, out hit);
if (hit.transform == null)
{
return;
}
T hitComponent = hit.transform.GetComponent<T>();
if (!movement && hit.transform != null)
{
CantMove(hitComponent);
}
}
protected abstract void CantMove<T>(T hit)
where T : Component;
}
/* /!\ THIS IS THE GAME MANAGER SCRIPT, I MISSED UP WITH THE NAMES /!\ */
/* /!\ THIS IS THE GAME MANAGER SCRIPT, I MISSED UP WITH THE NAMES /!\ */
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BordManager : MonoBehaviour {
public Game_Manager boardScript;
public static BordManager instance = null; // Checking if we haven't two GameManger
public int playerFood = 100;
[HideInInspector] public bool playerTurn = true;
public float turnDelay = 0.1f;
private int level = 3; // Level 3 == First enemy spawn
private List<Enemy> enemies = new List<Enemy>();
private bool enemyMoving;
void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
boardScript = GetComponent<Game_Manager>();
InitGame();
}
void InitGame()
{
enemies.Clear();
boardScript.SetupBord(level);
}
IEnumerator MoveEnemies()
{
enemyMoving = true;
yield return new WaitForSeconds(turnDelay);
if (enemies.Count == 0)
{
yield return new WaitForSeconds(turnDelay);
}
for (int i = 0; i < enemies.Count; i++)
{
enemies[i].MoveEnemy();
yield return new WaitForSeconds(enemies[i].moveTime);
}
playerTurn = true;
enemyMoving = false;
}
void Udapte()
{
if (playerTurn || enemyMoving)
{
return;
}
StartCoroutine(MoveEnemies());
}
public void AddEnemyToList(Enemy script)
{
enemies.Add(script);
}
public void GameOver()
{
enabled = false;
}
}
/* /!\ THIS IS MY BORD MANAGER SCRIPT, I MISSED UP WITH THE NAME /!\ */
/* /!\ THIS IS MY BORD MANAGER SCRIPT, I MISSED UP WITH THE NAME /!\ */
using System;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class Game_Manager : MonoBehaviour
{
[Serializable]
public class Count
{
public int maximum;
public int minimum;
public Count(int min, int max)
{
minimum = min;
maximum = max;
}
}
public int columns = 8;
public int rows = 8;
public int border = -1;
public Count wallsCount = new Count(3, 9);
public Count foodCount = new Count(1, 5);
public GameObject exit;
public GameObject[] wallsTiles; // The use of arrays is because...
public GameObject[] floorTiles; // ...we can pasted multiple floor, walls... design
public GameObject[] foodTiles;
public GameObject[] outerWallsTiles;
public GameObject[] enemyTiles; // Or diferent type of enemies
private Transform bordHandler;
private List<Vector3> possiblePositions = new List<Vector3>();
void Initialize()
{
possiblePositions.Clear(); // Clear our list
for (float x = 1; x < columns - 1; x++)
{
for (float y = 1; y < rows - 1; y++)
{
possiblePositions.Add(new Vector3(x, y, 0f)); // Add each possible position for our game elmts
}
}
}
void CreateTheFloor()
{ // Create our floor with != type of tiles
bordHandler = new GameObject("Board").transform;
for (int x = border; x <= columns; x++)
{
for (int y = border; y <= rows; y++)
{
GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)]; // By default our tile is a floor one
if (x == border || x == columns || y == border || y == rows)
{ // Except if we need to build walls
toInstantiate = outerWallsTiles[Random.Range(0, outerWallsTiles.Length)];
}
GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity);
instance.transform.SetParent(bordHandler);
}
}
}
Vector3 RandomPosition()
{ // Return a random position for our CreateObjectAtRandomPostion method
int randomIndex = Random.Range(0, possiblePositions.Count);
Vector3 randomPosition = possiblePositions[randomIndex];
possiblePositions.RemoveAt(randomIndex);
return randomPosition;
}
void CreateObjectAtRandomPosition(GameObject[] anArray, int minimum, int maximum)
{ // Create a given GameObject at a random position, stock in our possiblePositions List
int howMany = Random.Range(minimum, maximum + 1);
for (int i = 0; i < howMany; i++)
{
Vector3 position = RandomPosition();
GameObject tileChoice = anArray[Random.Range(0, anArray.Length)];
Instantiate(tileChoice, position, Quaternion.identity);
}
}
public void SetupBord(int level)
{ // Set up the Bord
CreateTheFloor();
Initialize();
int enemyCount = (int)Math.Log(level, 2f);
CreateObjectAtRandomPosition(enemyTiles, enemyCount, enemyCount); // Enemy spawn
CreateObjectAtRandomPosition(wallsTiles, wallsCount.minimum, wallsCount.maximum); // Obstacle spawn
CreateObjectAtRandomPosition(foodTiles, foodCount.minimum, foodCount.maximum); // Power ups spawn
Instantiate(exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity); // Create the Exit
}
}
Thanks again <3 <3
Comment
Your answer
Follow this Question
Related Questions
2D Rougelike Tutorial-Part 11 Player Does Not Move 0 Answers
Rect or Camera transform? 0 Answers
2D Rougelike Tutorial-Part 13 -Music Will Not Play 0 Answers
I need a good turorial to get me started - c# 2 Answers
How do i make part of an object disappear both in rendering and collision with triggers? 1 Answer