• 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 unity_wxrce1I-hrP38g · Feb 06, 2018 at 03:53 PM · charactercoroutinemovement script

Character Not Moving Properly NEED HELP

I'm attempting to write an AI for an enemy so that every second he moves to a new available square. I have him placed at a point on a tile map with a script attached to him. Since I don't want him to be trying to pick a new direction every frame I did some research and it appears the best option for this is to do a coroutine so that it's an action that doesn't execute every single frame. I've been trying for hours writing this different ways in the update function, in IEnumerator, having all sorts of checks turned on, but so far my character always seems to either be bouncing around like a pingpong ball or standing still. This is the code I have in the coroutine right now.

    IEnumerator move()
     {
         List<string> available = new List<string>();
 
         available.Add("North");
         available.Add("South");
         available.Add("East");
         available.Add("West");
 
         if (tilemap.GetTile(new Vector3Int(currentX, currentY + 1, 0)) == null || tilemap.GetTile(new Vector3Int(currentX, currentY + 1, 0)).name != floor || myStack.Contains(new int[] { currentX, currentY + 1 }))
         {
             available.Remove("north");
         }
         if (tilemap.GetTile(new Vector3Int(currentX, currentY - 1, 0)) == null || tilemap.GetTile(new Vector3Int(currentX, currentY - 1, 0)).name != floor || myStack.Contains(new int[] { currentX, currentY = 1 }))
         {
             available.Remove("south");
         }
         if (tilemap.GetTile(new Vector3Int(currentX - 1, currentY, 0)) == null || tilemap.GetTile(new Vector3Int(currentX - 1, currentY, 0)).name != floor || myStack.Contains(new int[] { currentX - 1, currentY }))
         {
             available.Remove("west");
         }
         if (tilemap.GetTile(new Vector3Int(currentX + 1, currentY, 0)) == null || tilemap.GetTile(new Vector3Int(currentX + 1, currentY, 0)).name != floor|| myStack.Contains(new int[] { currentX + 1, currentY }))
         {
             available.Remove("east");
         }
 
         yield return new WaitForSeconds(1f);
         if (available.Count > 0)
         {
             int random = Random.Range(0, (available.Count));
             string direction = available[random];
 
             switch (direction)
             {
                 case "north":
                     moveVector.y = 1f;
                     moveVector.x = 0;
                     currentY = currentY + 1;
                     break;
                 case "south":
                     moveVector.y = -1f;
                     moveVector.x = 0;
                     currentY = currentY - 1;
                     break;
                 case "east":
                     moveVector.y = 0;
                     moveVector.x = -1f;
                     currentX = currentX + 1;
                     break;
                 case "west":
                     moveVector.y = 0;
                     moveVector.x = 1f;
                     currentX = currentX - 1;
                     break;
             }
             transform.position = new Vector3Int(currentX, currentY, 0);
             moveState = MoveState.Walk;
             anim.SetFloat("moveX", moveVector.x);
             anim.SetFloat("moveY", moveVector.y);
             anim.SetBool("isMoving", true);
 
             myStack.Push(new int[] { currentX, currentY });
         }
         else
         {
             int[] test = myStack.Pop();
 
             currentX = test[0];
             currentY = test[1];
         }
     }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move Character to touched Position 2D (Without RigidBody and Animated Movement) 1 Answer

How do I make my player model rotate based on the direction he is walking. 0 Answers

Improving Click-to-Move Movement Script (2D) 0 Answers

FPS character spins uncontrollably ,1st person player spinning out of control 1 Answer

I have a colider error 1 Answer


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