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 /
This question was closed Feb 16, 2017 at 03:06 AM by VihanAgarwal for the following reason:

Duplicate Question

avatar image
0
Question by VihanAgarwal · Feb 14, 2017 at 06:33 AM · movement2d gamescript.randomenemy ai

Simple 2D Enemy AI

So I am looking to build a simple enemy AI. This is how I want it structured:

  • Spawn in the center of a gameObject with a BoxCollider2D that's a trigger

  • Move in any direction at a fixed speed.

  • Change direction every x amount of seconds

  • If it hits the trigger, simply flip the rotation.

Spawning/flipping on the trigger shouldn't be too hard so I am starting by simply getting a 2D sprite to randomly move and then change direction every x seconds. I saw a bunch of tutorials online that were 3D (these used Navmeshes) and some 2D tutorials that move only along one axis.

Does anyone know good resource/tools in Unity that will suit what I want?

Note: I have tried Mathf.PingPong and it sucks for what I want.

Comment
Add comment · Show 1
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 VihanAgarwal · Feb 15, 2017 at 07:13 PM 0
Share

UPDATE: This worked for me. transform.Translate (Vector3.forward Time.deltaTime moveSpeed, Space.Self);

3 Replies

  • Sort: 
avatar image
0

Answer by Tourist · Feb 14, 2017 at 08:17 AM

Create a script that spawn a game object with your AI script attached. Create your AI script that :

  • on Start : chooses a direction randomly

  • on Update : monitor the elapsed time since last direction choice made

  • on OnTriggerEnter : flip the current direction

I don't think you need anything else that your little hands and 10 minutes of your time, or maybe I have mistaken your request.

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
avatar image
0

Answer by VihanAgarwal · Feb 15, 2017 at 07:38 AM

@Tourist That's exactly what I wanted. I can easily do the first two. The only issue is I can't get my player to move in the same direction as it is facing. I have tried transform.Translate(transform.right * moveSpeed); but this doesn't work. Any help?

Comment
Add comment · Show 3 · 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
avatar image Tourist · Feb 15, 2017 at 01:57 PM 0
Share

transform.Translate(transform.forward moveSpeed Time.deltaTime);

avatar image VihanAgarwal · Feb 15, 2017 at 07:07 PM 0
Share

The time.deltaTime doesn't make a difference to the rotation. Any other suggestion?

avatar image Tourist VihanAgarwal · Feb 16, 2017 at 09:56 AM 0
Share

show some code please.

avatar image
0

Answer by AmoVires · Feb 15, 2017 at 02:01 PM

You can check what direction is it facing(rotation) and flip/move it accordingly. Or did I understand wrong?

Comment
Add comment · Show 1 · 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
avatar image VihanAgarwal · Feb 15, 2017 at 07:09 PM 0
Share

I am rotating it by a random value between -90 and 90. When I move it, it doesn't face the direction it is moving. I am using what @Tourist suggested right now.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Enemy Is not moving 1 Answer

Enemy Movement Problem! 1 Answer

How to create random movement in 2D 2 Answers

How to change width and color of a 2d sprite within script 1 Answer

2D sprite wavering 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