Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Copywright · Jun 19, 2013 at 04:15 PM · artificial intelligence2d ai

What's causing this in my 2D AI script?

I'm trying to make AI for my beat-em-up, so far just a simple following waypoints and chasing the player. I've gotten stuck at every corner, and I'd like some help from someone so I can see where I'm going wrong.

Here's my Code:

 void Update ()
     {
 
         if ((chaseTarget.position.x - spriteObject.transform.position.x) > attackRange)
         {
             Move(chaseTarget);
         }
         else if ((chaseTarget.position.x - spriteObject.transform.position.x) <= attackRange)
         {
             Move(chaseTarget);
         }
 
             
         else
         {
             Idle();
         }
 
     }
 
  void Move (Transform target)
     {
         //If the target is to the left of the sprite
         if (target.position.x < spriteObject.transform.position.x)  
         {
             velocity.x = -walkSpeed * Time.deltaTime;  //Move to the left
             sprite.scale = new Vector3(-1, 1, 1);      //Flip sprite to look left
             animController.walking = true;             //walking animation
         }
         //Alternatively, if the target is to the right
         else if(target.position.x > spriteObject.transform.position.x)      
         {
             velocity.x = walkSpeed * Time.deltaTime; //Move right
             sprite.scale = new Vector3(1, 1, 1);     //Flip to look to the right
             animController.walking = true;           //walking animation
         }

         //If the target is above the sprite
         if (target.position.y > spriteObject.transform.position.y)          
         {
             velocity.y = walkSpeed * Time.deltaTime;  //Move up
             animController.walking = true;            //waling animation
         }
         //Alternatively, if target is below sprite
         else if (target.position.y < spriteObject.transform.position.y)     
         {
             velocity.y = -walkSpeed * Time.deltaTime;  //Move down
             animController.walking = true;             //walking animation
         }
 
         worldMover.Move(velocity);                     //Move based on the velocity vector
 
     }

This code causes this:

http://i.imgur.com/HtEM8UE.gif

Any idea why?

Comment
Add comment · Show 6
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
avatar image Jamora · Jun 19, 2013 at 04:33 PM 0
Share

First, I would put any AI code in a coroutine, because you have much more control over how code is executed in a coroutine. Second, why are you multiplying velocity by Time.deltaTime? Time.deltaTime is the amount of time (in seconds) since the last frame.

avatar image Copywright · Jun 19, 2013 at 05:31 PM 0
Share

Just fixed the second point, no difference.

avatar image AntiLunchBox · Jun 19, 2013 at 05:51 PM 0
Share

A) Streets of Rage remake??!?!?!??!?!?!?!

B) You are sure that your walkspeed isn't massive? Try setting it to a really low number and see if it affects anything. It looks like in one update he's already passing the player by alot, and in the next he has to turn around the do the same thing over and over.

avatar image AntiLunchBox · Jun 19, 2013 at 05:54 PM 1
Share

This won't affect anything, but in your Update loop you're if statements will never reach Idle(). It'll always make it into the first or second if statement.

avatar image Copywright · Jun 19, 2013 at 06:06 PM 0
Share

Ah yeah, the walkspeed was pretty massive. I think I's a problem with my ranges.

Also, not an exact remake, but similar gameplay.

Show more comments

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

17 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

Related Questions

Setting up Unity Steer 0 Answers

Zombies AI 2 Answers

Sit automatically at waypoint 0 Answers

Unity ML Agent doesn't learn well on default examples 1 Answer

How can i hide from enemy behind a wall? 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