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 GabeGabe · Jan 22, 2015 at 12:03 PM · platformerenemy aipatrol

2D platformer game enemy AI (simple patrol) problem. How to solve it (C#) ?

I wanted to make enemies whom can patrol with a given distance.

Everything is good until it reaches distance<=0. I don't know what is wrong with it.

problem

This is my code:

 public class EnemyController : MonoBehaviour {
 
     public float speed = 3f;
 
     private float startingPositionX;
     public float endingPositionX = 5f;
     private float distance;
     private float originalDistance;
     private float originalPositionX;
 
 
 
     void Start(){
         startingPositionX = transform.position.x;
         distance = endingPositionX - startingPositionX;
         originalDistance = endingPositionX - startingPositionX;
         originalPositionX = transform.position.x;
     }
 
 
     void FixedUpdate(){
         if (distance >= 0) {
             rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
             distance = endingPositionX - transform.position.x;
             print (distance);
         }
         else if (distance <= 0) {
             startingPositionX = originalPositionX + endingPositionX;
             endingPositionX =originalPositionX;
             Flip ();
             print ("elerte");
             rigidbody2D.velocity = new Vector2 (-speed, rigidbody2D.velocity.y);
             distance = Mathf.Abs(endingPositionX - transform.position.x);
         }
     }
 
     void Flip(){
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
 }


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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Inok · Jan 22, 2015 at 12:21 PM

Add Debug.Log to variables for find what cause problem. I think that problem or with distance calculation, so add Debug.Log(distance); or delete "=" from "else if".

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 GabeGabe · Jan 22, 2015 at 12:36 PM 0
Share

I already did this but I used print("Elerte"). But I couldn't fix the problem

avatar image
0

Answer by screenname_taken · Jan 22, 2015 at 12:24 PM

Weird, my answer is gone after it was converted from a comment. Anyway... your issue is in the if statements. You have both of them to check for being equal to 0 as well as being less or more. So there is a moment that both checks are true, when your distance goes to 0. Remove the "=" from one of the two. Probably on the first if.

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 GabeGabe · Jan 22, 2015 at 12:38 PM 0
Share

I removed it but it still the same

avatar image zain-rafiq · May 27, 2015 at 10:03 AM 0
Share

Did you fix it? If you did, how?

avatar image GabeGabe · May 27, 2015 at 11:24 AM 0
Share

Yes, I fixed it but not with this code. I made a whole new one. And I did a little AI: https://www.assetstore.unity3d.com/en/#!/content/28819

(I think this can help a lot :) )

avatar image
0

Answer by GabeGabe · Aug 05, 2015 at 12:30 PM

SOLVED with this asset from the assetstore: https://www.assetstore.unity3d.com/en/#!/content/28819

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why is my enemy still patrolling with death animation even though i already killed it?,Why is my enemy still patrolling even though i already killed it? 1 Answer

How do I keep the enemies from moving to waypoints in other rooms? 1 Answer

"the object of type transform has been destroyed but you are still trying to access it" problem, 1 Answer

inspector,Component 0 Answers

Patrol for enemies not working correctly (2D) 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