Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Escherichia Coli · Jan 11, 2016 at 04:01 PM · 2dmovementcollidersimulation

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges