Hi everyone! My game is a turn-based dungeon crawler where enemies have a chance to spawn every time the player takes a step forwards. It works, but I feel like it's way too mish-mash and inefficient, not to mention potentially prone to bugs... When an enemy spawns, I prevent the player from moving, and spawn an enemy from a list, giving it health/attack values. Below is the script attached to my player that handles moving as well as spawning enemies - while I'm not having errors, I'm not too experienced and would really appreciate pointers in the right direction as to what I can improve :)
PlayerMovement.cs
-----
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public static bool CanMoveForward;
public static bool CanMoveBackward;
public static bool CanMoveLeft;
public static bool CanMoveRight;
public static bool CanRotateLeft;
public static bool CanRotateRight;
public static bool CanMoveForwardCache;
public static bool CanMoveBackwardCache;
public static bool CanMoveLeftCache;
public static bool CanMoveRightCache;
public static bool CanRotateLeftCache;
public static bool CanRotateRightCache;
public static bool InCombat = false;
public static int chancetospawn;
public static bool CanSpawnEnemy = true;
public Texture2D EnemyObject;
public RawImage EnemyImage;
public Texture2D Enemy1;
public Texture2D Enemy2;
public Texture2D Enemy3;
public Texture2D Enemy4;
public Texture2D Enemy5;
public Texture2D Enemy6;
public Texture2D Enemy7;
public Texture2D Enemy8;
public Texture2D Enemy9;
public Texture2D Enemy10;
public Texture2D EnemyNull;
public static int EnemyCurHealth;
public static int EnemyMaxHealth;
public static int EnemyDamage;
//Translation:
float movSpeed = 4.0f;
Vector3 pos;
Transform tr ;
public static bool moving = false;
//Rotation:
public static bool rotating = false;
public float rotSpeed = 360f;
float rotDegrees = 0f;
Quaternion rotToAngle ;
void Start () {
pos = transform.position;
tr = transform;
CanMoveForward = true;
CanMoveBackward = true;
CanMoveLeft = true;
CanMoveRight = true;
CanRotateLeft = true;
CanRotateRight = true;
}
void SpawnEnemy() {
CanMoveForwardCache = CanMoveForward;
CanMoveBackwardCache = CanMoveBackward;
CanMoveLeftCache = CanMoveLeft;
CanMoveRightCache = CanMoveRight;
CanRotateLeftCache = CanRotateLeft;
CanRotateRightCache = CanRotateRight;
CanMoveForward = false;
CanMoveBackward = false;
CanMoveLeft = false;
CanMoveRight = false;
CanRotateLeft = false;
CanRotateRight = false;
int i = Random.Range (1, 10);
if(i == 1) { EnemyObject = Enemy1; EnemyMaxHealth = 11; EnemyCurHealth = 11; EnemyDamage = 1; }
if(i == 2) { EnemyObject = Enemy2; EnemyMaxHealth = 12; EnemyCurHealth = 12; EnemyDamage = 1; }
if(i == 3) { EnemyObject = Enemy3; EnemyMaxHealth = 13; EnemyCurHealth = 13; EnemyDamage = 1; }
if(i == 4) { EnemyObject = Enemy4; EnemyMaxHealth = 14; EnemyCurHealth = 14; EnemyDamage = 2; }
if(i == 5) { EnemyObject = Enemy5; EnemyMaxHealth = 15; EnemyCurHealth = 15; EnemyDamage = 2; }
if(i == 6) { EnemyObject = Enemy6; EnemyMaxHealth = 16; EnemyCurHealth = 16; EnemyDamage = 3; }
if(i == 7) { EnemyObject = Enemy7; EnemyMaxHealth = 17; EnemyCurHealth = 17; EnemyDamage = 3; }
if(i == 8) { EnemyObject = Enemy8; EnemyMaxHealth = 18; EnemyCurHealth = 18; EnemyDamage = 3; }
if(i == 9) { EnemyObject = Enemy9; EnemyMaxHealth = 19; EnemyCurHealth = 19; EnemyDamage = 4; }
if(i == 10) { EnemyObject = Enemy10; EnemyMaxHealth = 19; EnemyCurHealth = 19; EnemyDamage = 5; }
EnemyImage.GetComponent().texture = EnemyObject;
InCombat = true;
}
void EnemySpawned() {
chancetospawn = 0;
CanMoveForward = CanMoveForwardCache;
CanMoveBackward = CanMoveBackwardCache;
CanMoveLeft = CanMoveLeftCache;
CanMoveRight = CanMoveRightCache;
CanRotateLeft = CanRotateLeftCache;
CanRotateRight = CanRotateRightCache;
PlayerStats.CurrentXP += 12;
Debug.Log("Enemy defeated. You have gained 12 XP points.");
EnemyImage.GetComponent().texture = EnemyNull;
EnemyDamage = 0;
EnemyCurHealth = 0;
EnemyMaxHealth = 0;
InCombat = false;
}
void Update () {
if(CameraLook.AtLeftEnd == true && Input.GetButtonUp("Fire2")) {
RotLeft();
}
if(CameraLook.AtRightEnd == true && Input.GetButtonUp("Fire2")) {
RotRight();
}
if (InCombat && EnemyCurHealth <= 0) {
EnemySpawned();
}
Debug.DrawRay(transform.position, transform.forward, Color.red);
//Input:
if (!moving && !rotating) {
if (Input.GetKey(KeyCode.D) && tr.position == pos && CanMoveRight) {
MoveRight();
} else if (Input.GetKey(KeyCode.A) && tr.position == pos && CanMoveLeft) {
MoveLeft();
} else if (Input.GetKey(KeyCode.W) && tr.position == pos && CanMoveForward) {
MoveForward();
} else if (Input.GetKey(KeyCode.S) && tr.position == pos && CanMoveBackward) {
MoveBackward();
} else if (Input.GetKey(KeyCode.Q) && tr.position == pos && CanRotateLeft) {
RotLeft();
} else if (Input.GetKey(KeyCode.E) && tr.position == pos && CanRotateRight) {
RotRight();
}
}
//Translation:
if (moving) {
if (Vector3.Distance(transform.position,pos) <0.05f){
transform.position = pos;
moving=false;
// Debug.Log("FINISHED MOVE!!!!!!!!");
if (chancetospawn == 1) {
SpawnEnemy();
}
} else {
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
}
}
//Rotation:
if (rotating) {
if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
transform.rotation = rotToAngle;
rotating=false;
} else {
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
}
}
if(EnemyCurHealth <= 0) {
EnemyCurHealth = 0;
}
}
public void MoveForward() {
if (tr.position == pos && CanMoveForward) {
pos += transform.forward;
moving=true;
chancetospawn = Random.Range(1, 10);
}
}
public void MoveBackward() {
if (tr.position == pos && CanMoveBackward) {
pos += -transform.forward;
moving=true;
chancetospawn = Random.Range(1, 10);
}
}
public void MoveLeft() {
if (tr.position == pos && CanMoveLeft) {
pos += -transform.right;
moving=true;
chancetospawn = Random.Range(1, 10);
}
}
public void MoveRight() {
if (tr.position == pos && CanMoveRight) {
pos += transform.right;
moving=true;
chancetospawn = Random.Range(1, 10);
}
}
public void RotLeft() {
if (tr.position == pos && CanRotateLeft) {
rotDegrees -= 90f;
rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotating = true;
}
}
public void RotRight() {
if (tr.position == pos && CanRotateRight) {
rotDegrees += 90f;
rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotating = true;
}
}
}
↧