Cell-Like Movement on a 2D Simulation: How to stop erratic movement and add colliders
Hello, I'm a total begginer in coding and game-making, but I adventured myself in trying to produce a cell-like simulation, where the red cells chase and eat the green ones, who try to escape. I can show you the code, and you can try for yourself. But I have big issues here, and I just don't know how to solve them.
1) The cells have an erratic movement, since their position is updated and changed every frame. For example, in frame 1 I tell them to move 10 units up, but on frame 2 I change that value to -10, and so on, so they are constantly shifting and changing direction. Altough this is kinda like a real cell movement, I would like to have controll over this.
2) The code is a mess. I didn't knew how to make them leave the corners, so I made some pseudo-coding. It's the definition of "If it is stupid and it works, it ain't stupid." The thing is: they still get stuck in the corners sometimes, so I'm thinking that there's got to be a way to make this behaviour easier.
3) Finally, if I have 2 green cells, even tough they have Circle Colider 2D on each one, they still go over each other. This should be fairly simple to fix, but I don't know how.
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public GameObject[] enemies;
public float walking;
public float speed;
public float detectionRadius;
public enum State {chasing, running};
public State stateIs;
private GameObject closestEnemy;
private float dist = Mathf.Infinity;
private float runningX;
private float runningY;
private string corner;
void Update() {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
FindNearest ();
if (this.stateIs == State.chasing) {
Vector3 increase = new Vector3 (closestEnemy.transform.position.x,
closestEnemy.transform.position.y,
closestEnemy.transform.position.z);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
if (this.stateIs == State.running) {
if (thisX < 8.5) {
if (thisY < 5) {
Debug.Log ("UnStucking Bottom Left");
Stuck ("leftBottom");
} else if (thisY > 10) {
Stuck ("leftTop");
//Debug.Log ("UnStucking Top Left");
} else {
Decision ();
}
}
else if (thisX > 21.5) {
if (thisY < 6.5) {
Stuck ("rightBottom");
//Debug.Log ("UnStucking Bottom Right");
} else if (thisY > 10) {
Stuck ("rightTop");
//Debug.Log ("UnStucking Top Right");
} else {
Decision ();
}
}
else {
Decision ();
}
}
}
void Decision() {
float dist = (this.transform.position - closestEnemy.transform.position).sqrMagnitude;
if (dist > detectionRadius) {
Idle ();
Debug.Log ("Idle");
} else {
Run ();
Debug.Log ("Run");
}
}
void Run() {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
FindNearest ();
if (closestEnemy.transform.position.x < thisX && thisX < 22) {
runningX = this.transform.position.x + 1;
} else if (closestEnemy.transform.position.x > thisX && thisX > 7.7) {
runningX = this.transform.position.x - 1;
}
if (closestEnemy.transform.position.y < thisY && thisY < 11.5) {
runningY = this.transform.position.y + 1;
} else if (closestEnemy.transform.position.y > thisY && thisY > 5){
runningY = this.transform.position.y - 1;
}
Vector3 increase = new Vector3 (runningX, runningY, 0.0f);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
void Stuck(string corner) {
if (corner == "leftBottom") {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
runningX = thisX + Random.Range(10f,17f);
runningY = thisY + Random.Range(10f,17f);
Vector3 increase = new Vector3 (runningX,runningY,0.0f);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
else if (corner == "leftTop") {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
runningX = thisX + Random.Range(10f,17f);
runningY = thisY - Random.Range(10f,17f);
Vector3 increase = new Vector3 (runningX,runningY,0.0f);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
else if (corner == "rightBottom") {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
runningX = thisX - Random.Range(1f,7f);
runningY = thisY + Random.Range(1f,7f);
Vector3 increase = new Vector3 (runningX,runningY,0.0f);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
else if (corner == "rightTop") {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
runningX = thisX - Random.Range(1f,7f);
runningY = thisY - Random.Range(1f,7f);
Vector3 increase = new Vector3 (runningX,runningY,0.0f);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
}
void FindNearest() {
float closestDist = Mathf.Infinity;
foreach (GameObject enemy in enemies) {
float dist = (this.transform.position - enemy.transform.position).sqrMagnitude;
if (dist < closestDist) {
closestDist = dist;
closestEnemy = enemy;
}
}
}
void Idle() {
float thisX = this.transform.position.x;
float thisY = this.transform.position.y;
//isCenter
if (thisX < 22 && thisX > 8 && thisY < 11.5 && thisY > 4) {
runningX = this.transform.position.x + Random.Range (-1.0f, 1.0f);
runningY = this.transform.position.y + Random.Range (-1.0f, 1.0f);
}
//MoveToCenter
//isBottom
else if (thisY < 4) {
runningX = this.transform.position.x + Random.Range (-1.0f, 1.0f);
runningY = this.transform.position.y + 5;
}
//isTop
else if (thisY > 11.5) {
runningX = this.transform.position.x + Random.Range (-1.0f, 1.0f);
runningY = this.transform.position.y - 5;
}
//isLeft
else if (thisX < 8) {
runningX = this.transform.position.x + 5;
runningY = this.transform.position.y + Random.Range (-1.0f, 1.0f);
}
//isRight
else if (thisX > 22) {
runningX = this.transform.position.x - 5;
runningY = this.transform.position.y + Random.Range (-1.0f, 1.0f);
}
//Debug.Log ("Moving " + runningX + " units horizontal.");
//Debug.Log ("Moving " + runningY + " units vertical.");
Vector3 increase = new Vector3 (runningX, runningY, 0.0f);
this.transform.position = Vector3.MoveTowards (this.transform.position, increase, speed);
}
}
Your answer
Follow this Question
Related Questions
2D Top Down, Enemy facing player slow rotation issue. Vector gets messed up after collision. 3 Answers
Player won't stop moving when key is released 1 Answer
My 2D object keeps on falling through the terrain (cube) 1 Answer
Move 2D object forward 1 Answer
Adding small acceleration to 2d top down movement game? 0 Answers