My character can move forwards, backwards, left, right, and rotate 90 degrees left and right. It’s a grid-based game, and I have everything in order for my prototype, except one issue: The enemies, what have a chance to spawn on movement, end up spawning before the move action is complete, so it spawns then the character moves forwards and ends up inside the enemy object… Additionally, if it spawns from moving backwards or sideways, I want the player to be rotated to face that way before the movement restriction is activated… It will be more clear when you see my code below. This is the script that handles most of these things I’ve specified. I’d greatly appreciated it if you can help me have the enemy spawn after the player has finished moving, and to rotate to face the enemy if it spawned from moving at a direction other than forwards. Thanks <3
using UnityEngine;
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 int chancetospawn;
public static bool CanSpawnEnemy = true;
public GameObject Enemy1;
public GameObject Enemy2;
public GameObject Enemy3;
public GameObject Enemy4;
public GameObject Enemy5;
public GameObject Enemy6;
public GameObject Enemy7;
public GameObject Enemy8;
public GameObject Enemy9;
public GameObject Enemy10;
public static GameObject EnemyObject;
public GameObject EnemySpawner;
//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;
CanMoveForward = false;
CanMoveBackward = false;
CanMoveLeft = false;
CanMoveRight = false;
CanRotateLeft = false;
CanRotateRight = false;
int i = Random.Range (1, 10);
if(i == 1) {
EnemyObject = Enemy1;
EnemyStats.enemyhealth = 20;
EnemyStats.enemydamage = 3;
}
if(i == 2) {
EnemyObject = Enemy2;
EnemyStats.enemyhealth = 25;
EnemyStats.enemydamage = 5;
}
if(i == 3) {
EnemyObject = Enemy3;
EnemyStats.enemyhealth = 30;
EnemyStats.enemydamage = 5;
}
if(i == 4) {
EnemyObject = Enemy4;
EnemyStats.enemyhealth = 35;
EnemyStats.enemydamage = 6;
}
if(i == 5) {
EnemyObject = Enemy5;
EnemyStats.enemyhealth = 40;
EnemyStats.enemydamage = 7;
}
if(i == 6) {
EnemyObject = Enemy6;
EnemyStats.enemyhealth = 50;
EnemyStats.enemydamage = 8;
}
if(i == 7) {
EnemyObject = Enemy7;
EnemyStats.enemyhealth = 100;
EnemyStats.enemydamage = 10;
}
if(i == 8) {
EnemyObject = Enemy8;
EnemyStats.enemyhealth = 100;
EnemyStats.enemydamage = 20;
}
if(i == 9) {
EnemyObject = Enemy9;
EnemyStats.enemyhealth = 100;
EnemyStats.enemydamage = 20;
}
if(i == 10) {
EnemyObject = Enemy10;
EnemyStats.enemyhealth = 100;
EnemyStats.enemydamage = 20;
}
Instantiate(EnemyObject, EnemySpawner.transform.position, Quaternion.identity);
}
// Update is called once per frame
void Update () {
// Debug.Log (CanSpawnEnemy);
Debug.DrawRay(transform.position, transform.forward, Color.red);
//Input:
if (!moving && !rotating) {
if (Input.GetKey(KeyCode.D) && tr.position == pos && CanMoveRight) {
pos += transform.right;
moving=true;
chancetospawn = Random.Range(1, 10);
if(chancetospawn == 1 && CanSpawnEnemy == true) {
SpawnEnemy();
}
} else if (Input.GetKey(KeyCode.A) && tr.position == pos && CanMoveLeft) {
pos += -transform.right;
moving=true;
chancetospawn = Random.Range(1, 10);
if(chancetospawn == 1 && CanSpawnEnemy == true) {
SpawnEnemy();
}
} else if (Input.GetKey(KeyCode.W) && tr.position == pos && CanMoveForward) {
pos += transform.forward;
moving=true;
chancetospawn = Random.Range(1, 10);
if(chancetospawn == 1 && CanSpawnEnemy == true) {
SpawnEnemy();
}
} else if (Input.GetKey(KeyCode.S) && tr.position == pos && CanMoveBackward) {
pos += -transform.forward;
moving=true;
chancetospawn = Random.Range(1, 10);
if(chancetospawn == 1 && CanSpawnEnemy == true) {
SpawnEnemy();
}
} else if (Input.GetKey(KeyCode.Q) && tr.position == pos && CanRotateLeft) {
rotDegrees -= 90f;
rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotating = true;
} else if (Input.GetKey(KeyCode.E) && tr.position == pos && CanRotateRight) {
rotDegrees += 90f;
rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotating = true;
}
}
//Translation:
if (moving) {
if (Vector3.Distance(transform.position,pos) <0.05f){
transform.position = pos;
moving=false;
// print ("FINISHED MOVE");
} 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;
// print ("FINISHED TURN");
} else {
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
}
}
}
}