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 alienx2 · Nov 14, 2010 at 08:18 AM · 2daienemytarget

2D Enemy AI X-way only

I tried learn tutorial from youtube that about "myTransform.position += myTransform.position.forward moveSpeed Time.deltaTime;" and its for XYZ.

then i tried made for 2D Enemy walk X-way only but the problem is always enemy went abnormal and run away from the screen view.

here code:

function enemymovement() {

 enemytransform.transform.position.x += targetat.transform.position.x * moveSpeed * Time.deltaTime;

 if (enemytransform.transform.position.x >= HiddenCubeLeft.transform.position.x) // Limit +X
 {
 enemytransform.transform.position.x = HiddenCubeLeft.transform.position.x + 0.1;
 return;
 }
 else if (enemytransform.transform.position.x >= HiddenCubeRight.transform.position.x) // Limit -X
 {
 enemytransform.transform.position.x = HiddenCubeRight.transform.position.x - 0.1;
 return;
 }

}

I did inserted "enemymovement();" in function Update.

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 denewbie · Nov 14, 2010 at 10:35 AM

There are a few problems...

1) I think there's something wrong with your greater than and lesser then signs... Basically they re the same for boths side. I m just guessing cause i fdont know what "HiddenCudeLeft" and "HiddenCubeRight" is...

if (enemytransform.transform.position.x >= HiddenCubeLeft.transform.position.x) else if (enemytransform.transform.position.x >= HiddenCubeRight.transform.position.x)

prob should be something like

if (enemytransform.transform.position.x >= HiddenCubeLeft.transform.position.x) else if (enemytransform.transform.position.x <= HiddenCubeRight.transform.position.x)

2) This is prob the problem with it runing all over the place.

enemytransform.transform.position.x += targetat.transform.position.x moveSpeed Time.deltaTime;

... should just be...

enemytransform.transform.position.x += moveSpeed * Time.deltaTime;

3) also, I ll prob structure the function like this...

function enemymovement() {

 if (enemytransform.transform.position.x &gt;= HiddenCubeLeft.transform.position.x) // Limit +X
        enemytransform.transform.position.x = HiddenCubeLeft.transform.position.x + 0.1;

 else if (enemytransform.transform.position.x &lt;= HiddenCubeRight.transform.position.x) // Limit -X
        enemytransform.transform.position.x = HiddenCubeRight.transform.position.x - 0.1;


 enemytransform.transform.position.x += moveSpeed * Time.deltaTime;
 return;

}

Comment
Add comment · Show 4 · 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 alienx2 · Nov 14, 2010 at 12:45 PM 0
Share

how about targetat and follow target in x-way?

avatar image alienx2 · Nov 14, 2010 at 02:31 PM 0
Share

"HiddenCudeLeft" and "HiddenCubeRight" means like waypoint that enemy dont walk over line.Take a look here screenshot: Behind the scene: https://www.sugarsync.com/pf/D109027_6729738_68567 | Game Preview: https://www.sugarsync.com/pf/D109027_6729738_68560

avatar image denewbie · Nov 14, 2010 at 05:53 PM 0
Share

Do you want to make your "enemy" walk left to right continuously and automatically or do you want it to follow your targettat?

avatar image alienx2 · Nov 16, 2010 at 08:08 AM 0
Share

yes.. like that... both of them...

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

No one has followed this question yet.

Related Questions

How do I go about making a 2d platform enemy ai 1 Answer

enemy AI patrol not working 1 Answer

Flipping enemy 2d character to the side he is walking? 1 Answer

How to create a simple Enemy AI for a 2D game? Mine isn't working. . . 1 Answer

How to make basic AI in a 2d game? 4 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