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 /
avatar image
0
Question by harpoaian · Sep 05, 2017 at 01:58 PM · 2d-platformer2d-physics2d-gameplayenemyai

Enemy is moving opposite of the player 2D

Good morning everyone,

As the title says I have a flying enemy that works like it is intended to but messes up. My enemy detects the player via OverlapCircle and he does what he is supposed to move towards the player and then once the player is in the raycast for shooting it shoots. Here is the problem, when the player changes the way they are facing the enemy goes bonkers. It stutters in place and changes the way it faces constantly. When the player gets out of the Overlap circle the enemy starts moving away from the player and not towards it. Here is the code that is related to this issue.

 void Vision()
     {
         playerInRange = Physics2D.OverlapCircle(transform.position, playerRange, 1 << LayerMask.NameToLayer("Player"));
 
         spotted = Physics2D.Linecast(sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("Player"));
 
       // flyingStart = Physics2D.Linecast(flightStart.position, flightEnd.position, 1 << LayerMask.NameToLayer("Player"));
 
         if (spotted == true)
         {
             SetState(AerialFighterStates.FIRE);
             //Debug.Log("I should fire!");
             if (playerInRange == true)
             {
 
                 SetState(AerialFighterStates.PATROL);
                // Debug.Log("I should Start flying!");
 
             }
 
         }
         else
         {
             SetState(AerialFighterStates.IDLE);
             //Debug.Log("I should be in idle if out of shooting range");
         }
 
         if (playerInRange == true)
         {
 
             SetState(AerialFighterStates.PATROL);
            // Debug.Log("I should Start flying!");
 
             if (spotted == true)
             {
                 SetState(AerialFighterStates.FIRE);
                 //Debug.Log("I should fire!");
 
             }
         }
 
         else
         {
             SetState(AerialFighterStates.IDLE);
             //Debug.Log("I should be in idle if out of flying range!");
         }
     }
 void Patrol()
     {     
         transform.position = Vector3.MoveTowards(transform.position, thePlayer.transform.position, moveSpeed * Time.deltaTime);
         
         colliding = Physics2D.Linecast(sightStart.position, wallEnd.position, detectWhat);
 
         if (colliding == true)
         {
             transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
             moveSpeed *= -1;
 
             facingRight = !facingRight;
 
         }
 
         if((thePlayer.transform.position.x < transform.position.x && thePlayer.transform.localScale.x < 0) || (thePlayer.transform.position.x > transform.position.x && thePlayer.transform.localScale.x > 0))
         {
             transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
             moveSpeed *= -1;
             facingRight = !facingRight;
             return;
         }
Comment
Add comment · Show 3
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 harpoaian · Sep 08, 2017 at 01:23 PM 0
Share

not sure if it works like this but BU$$anonymous$$P!

avatar image harpoaian · Sep 27, 2017 at 12:16 PM 0
Share

still can't figure this out bump

avatar image RiQQ92 · Sep 27, 2017 at 01:25 PM 0
Share

I think it comes from the code where you check on which side the player is, you might have meant to use 'transform.localScale' ins$$anonymous$$d of 'thePlayer.transform.localScale'. It's bit hard to follow so i may have got it wrong.

Additionally you could simplify your Vision methods 'if else' clauses. This should do the same with less confusion:

          if (spotted == true)
          {
              SetState(AerialFighterStates.FIRE);
              //Debug.Log("I should fire!");
          }
          else if (playerInRange == true)
          {
              SetState(AerialFighterStates.PATROL);
             // Debug.Log("I should Start flying!");
          }
          else
          {
              SetState(AerialFighterStates.IDLE);
              //Debug.Log("I should be in idle if out of flying range!");
          }

Unless there is some mysterious magic in 'SetState()', then this might not work. Hope this helps, cheers.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Cuttlas-U · Sep 27, 2017 at 01:30 PM

hi;

maybe multiple the move speed by a negative number is changing the direction :

 moveSpeed *= -1;
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 harpoaian · Sep 30, 2017 at 04:32 PM 0
Share

that was it! Thank you it's always the simple things that I miss lol All I had to do was change the moveSpeed to 1 ins$$anonymous$$d of -1.

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

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

Related Questions

How to keep Y velocity normal? 1 Answer

Jump freeze when horizontal movement is being held down 1 Answer

How To Make My 2D Character Jump Gradually 2 Answers

"Dash-Move" only work some times. 1 Answer

Having some stuck issues on the 2D infinite runner 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