• 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
1
Question by catchthefloaty · Jun 15, 2014 at 06:20 AM · aienemysimple

Basic AI script. need help.

ok, so im working on a basic 2d AI script. The enemies are to simple home in on the player. however they dont go straight there, but have a steering limit... basically they have to actually turn around instead of doing an instant 180. however i keep getting really weird behavior and would appreciate any help.

void Update () { //find direction to player

     direction = Vector3.Normalize(player.transform.position - this.transform.position);
     float rot_z = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
     target.transform.Rotate(0, 0, rot_z - 90f);
     
     //determine left or right
     if (Mathf.Abs(target.transform.rotation.eulerAngles.z - transform.rotation.eulerAngles.z) < 179) {
         left = true;
     }
     else { left = false; }

     /*if (Mathf.Abs(target.transform.rotation.eulerAngles.z - transform.rotation.eulerAngles.z) < turnSpeed) {
         turnSpeed = 0;
     }*/
     if (!left) {

         turnSpeed += turnAccel;
     }
     else if (left) {
         turnSpeed -= turnAccel;
     }

     if (turnSpeed > turnAccelMax) { turnSpeed = turnAccelMax; }
     if (turnSpeed < -turnAccelMax) { turnSpeed = -turnAccelMax; }
     speed += speedAccel;
     speed -= Mathf.Abs(turnSpeed);
     if (speed <= 0) { speed = 0; }

     if (speed > speedMax) { speed = speedMax; }

     //target.transform.position = transform.position;
     //target.transform.rotation = transform.rotation;

     //target.transform.Rotate(0, 0, -turnSpeed);
     target.transform.RotateAround(transform.position, Vector3.forward, turnSpeed);
     target.transform.Translate(new Vector3(direction.x * speed * drag, direction.y * speed * drag, 0), Space.Self);
     
     // currently target does not rotate correctly
     rigidbody.MovePosition(new Vector3(target.transform.position.x, target.transform.position.y, 0));
     
     transform.Rotate(0,0,-turnSpeed);

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sedativechunk · Jun 15, 2014 at 08:57 AM

That is a lot of code to sort through, but one thing I would like to point out immediately is a couple huge things. Number one, physics (like moving around, rotating, etc.) should be done in the FixedUpdate() method, NOT the Update() method. That could be causing your problem right off the bat. FixedUpdate is called every physics step and is what you should use when you are dealing with the physics of the game.

Number two, all your movement speeds need to be multiplied by the delta time, like this for example: turnSpeed += turnAccel time.deltaTime*;

Notice the code I added there? This will now tell unity "I want to move my object by turnAccel units per second". The * time.deltaTime is the "per second" in pseudocode. Everything that is dealing with updating the position/rotation of anything always needs to be multiplied by time.deltaTime. Not doing so could cause problems like the one you are experiencing now as well as framerate and other physics issues.

Number two, it's possible the speed at which your A.I. is turning is set way too high. You might want to try super small units first like 0.01f and work your way up. And when programming A.I., you should code and test one thing at a time. For example, if your enemy is on the left of the screen and you want him to rotate towards the right, then you should first write JUST the code to do that, test, it, then make sure it works before you do the code for the right side. I hope this steers you in the right direcitons.

Comment
Add comment · Share
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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Make player not be seen by AI, when player in foilage and shadows. 1 Answer

AI enemy scripting help 1 Answer

Shooting Damage Help 1 Answer

Stay Back! 2 Answers

How to stop enemy shooting through 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