Issue with manual collision and mobs spawning...

My character has four colliders on every side that detect if the player is colliding with a wall to stop movement in that given direction. My game also has a random enemy encounter mechanic, where every step moved has a 1 out of 10 chance to trigger a sequence where all movement is frozen, an enemy spawns, and after the enemy is destroyed, movement is resumed again. I have several scripts that control this. My problem is, when a mob spawns, movement is not stopped instantly; rather, the player moves one block forward. Is there a possible way to make the random roll at the end of the movement cycle so the player’s movement is halted INSTANTLY after the mob has spawned?

Thanks in advance!

Here are all the scripts associated with the issue:

FRONT COLLISION SYSTEM (other three [back, left, right] are very similar):

using UnityEngine;
using System.Collections;

public class FrontCollisionSystem : MonoBehaviour {

	public bool CanPickupItem = false;

void OnTriggerStay(Collider other) {
		if(other.CompareTag("wall")) {
			PlayerMovement.CanMoveForward = false;
		}

		if(other.CompareTag("item")) {
			CanPickupItem = true;
		}
	}

	void OnTriggerExit(Collider other) {
		if(other.CompareTag("wall")) {
			PlayerMovement.CanMoveForward = true;
			CanPickupItem = false;
		}
	}

}

PLAYER MOVEMENT:

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;
		}
		if(i == 2) {
			EnemyObject = Enemy2;
			EnemyStats.enemyhealth = 25;
		}
		if(i == 3) {
			EnemyObject = Enemy3;
			EnemyStats.enemyhealth = 30;
		}
		if(i == 4) {
			EnemyObject = Enemy4;
			EnemyStats.enemyhealth = 35;
		}
		if(i == 5) {
			EnemyObject = Enemy5;
			EnemyStats.enemyhealth = 40;
		}
		if(i == 6) {
			EnemyObject = Enemy6;
			EnemyStats.enemyhealth = 50;
		}
		if(i == 7) {
			EnemyObject = Enemy7;
			EnemyStats.enemyhealth = 100;
		}
		if(i == 8) {
			EnemyObject = Enemy8;
			EnemyStats.enemyhealth = 100;
		}
		if(i == 9) {
			EnemyObject = Enemy9;
			EnemyStats.enemyhealth = 100;
		}
		if(i == 10) {
			EnemyObject = Enemy10;
			EnemyStats.enemyhealth = 100;
		}

		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);
			}
		}
	}

}

ENEMY STATS:

using UnityEngine;
using System.Collections;

public class EnemyStats : MonoBehaviour {

	public static int enemyhealth = 100;

	// Use this for initialization
	void Start () {
//		PlayerStats.inCombat = true;
	}
	
	// Update is called once per frame
	void Update () {
		if(enemyhealth <= 1) {
			PlayerMovement.chancetospawn = 0;

			PlayerMovement.CanMoveForward = PlayerMovement.CanMoveForwardCache;
			PlayerMovement.CanMoveBackward = PlayerMovement.CanMoveBackwardCache;
			PlayerMovement.CanMoveLeft = PlayerMovement.CanMoveLeftCache;
			PlayerMovement.CanMoveRight = PlayerMovement.CanMoveRightCache;
			PlayerMovement.CanRotateLeft = true;
			PlayerMovement.CanRotateRight = true;

			PlayerStats.CurrentXP += 50;

			if (gameObject != null) {
				Destroy(this.gameObject);
			}
		}
	}
}

Thanks in advance! :smiley:

Sorry I can’t prune through all that code but the general idea is to have a flag that controls whether or not the player can move.

bool CanMove = true;

void OnTriggerEnter()
{
if (Random.Range(0,10) == 0)
    {
        CanMove = false;
    }
}

void Update()
{
    if (CanMove)
    {
        DoMoveStuff();
    }
}

When the enemy dies and you want the player to be able to move again, just set CanMove = true.