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 /
  • Help Room /
avatar image
0
Question by Shim-Slady · May 06, 2021 at 05:22 PM · spriteunity 2daispritespatrol

Sprite AI rapidly flipping to try and face the player. This is driving me insane.

So I've got a script that creates a basic FOV mesh, attaches it to the enemy, then sets the aim direction. If the player enters the FOV, I want the aim direction to target the player - but it keeps rapidly flipping back and forth instead.

FOV mesh based off Code Monkey's tutorial: https://www.youtube.com/watch?v=CSeUMTaNFYk&t=786s

I'm sure there are better ways to accomplish my goals in code, but I'm relatively new to C#. Hoping someone can answer this shout into the wind.

 public class EnemyController : MonoBehaviour
 {
 
     [SerializeField] private EnemyFOV enemyFOV;
     [SerializeField] private PlayerChange playerChange;
     [SerializeField] private PlayerMovement playerMovement;
     public float speed;
     public float runSpeed;
     private float distanceToStop = 0.1f;
     private float waitTime;
     public float startWaitTime = 3f;
     public Transform[] waypoints;
     public Rigidbody2D rb2D;
     int waypointIndex = 0;
     private float enemyPos;
 
     private Vector3 patrolTarget;
     private Vector3 playerPosition;
     private Vector3 aimDirection;
     private bool isFound;
 
 
     void Start()
     {
         isFound = false;
 
         waitTime = startWaitTime;
         
         // Spawn at first waypoint
         transform.position = waypoints[waypointIndex].transform.position;
     }
 
     void Update()
     {
         
         rb2D = GetComponent<Rigidbody2D>();
 
         // Get local scale and velocity
         enemyPos = transform.localScale.x;
         var localVelocity = transform.InverseTransformDirection(rb2D.velocity);
         
         // Get player position
         playerPosition = GameObject.FindGameObjectWithTag("Player").transform.position;
 
         if (isFound == false)
         {
             FindTargetPlayer();
             EnemyPatrol();
 
             if (localVelocity.x > 0.5) 
             {
                 transform.localScale = new Vector3 (1, 1, 1);
                 aimDirection = new Vector3 (1, 0, 0);
             } 
         
             else if (localVelocity.x < -0.5) 
             { 
                 transform.localScale = new Vector3 (-1, 1, 1);
                 aimDirection = new Vector3 (-1, 0, 0);
             }
         }
         
         if (isFound == true)
         {
             aimDirection = playerPosition;
             
             StartCoroutine(FollowPlayer()); 
     
         }
 
         // Set origin point for FOV mesh
         enemyFOV.SetOrigin(gameObject.transform.position);
         enemyFOV.SetAimDirection(aimDirection);
 
     }
 
     void EnemyPatrol()
     {
         // Set target waypoint
         patrolTarget = waypoints[waypointIndex].transform.position;
 
         // Set direction and apply force
         var direction = Vector3.zero;
         if(Vector3.Distance(transform.position, patrolTarget) > distanceToStop)
         {
             direction = patrolTarget - transform.position;
             rb2D.AddRelativeForce(direction.normalized * speed * Time.deltaTime, ForceMode2D.Force);
         }
 
         // Go to next waypoint
         if(Vector3.Distance(transform.position, patrolTarget) < distanceToStop)
         {
             if(waitTime <=0)
             {
                 waypointIndex += 1;
                 waitTime = startWaitTime;
             }
             else
             {
                 waitTime -= Time.deltaTime;
             }
         }
         
         if(waypointIndex == waypoints.Length)
         {
             waypointIndex = 0;
         }
 
     }
 
     void FindTargetPlayer()
     { 
         if (Vector3.Distance(transform.position, playerPosition) < 15)
         {
             Vector3 directionToPlayer = (playerPosition - transform.position).normalized;
             if (Vector3.Angle(aimDirection, directionToPlayer) < 90 / 2)
             {
                 RaycastHit2D raycasthit2D = Physics2D.Raycast(transform.position, directionToPlayer, 15);
                 if (raycasthit2D.collider != null)
                 {
                     if (raycasthit2D.collider.gameObject.tag == "Player")
                     {
                         if(playerChange.playerHiding == false)
                         {
                             isFound = true;
                         }
                     }
                     else
                     {
                         isFound = false;
                     }
                 }
             }
         }
     }
 
     IEnumerator FollowPlayer()
     {
         
         var direction = Vector3.zero;
         if(Vector3.Distance(transform.position, playerPosition) > distanceToStop)
         {
             direction = playerPosition - transform.position;
             rb2D.AddRelativeForce(direction.normalized * runSpeed * Time.deltaTime, ForceMode2D.Force);
         }
 
         yield return isFound = false;
     }
 
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

271 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 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 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 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

Gray Pixels put on sprites that weren't there before. 0 Answers

Cycling through array not working. Pulling my hair over this one. 0 Answers

How do I get the positions of the corners of a sprite? 2 Answers

Sprite Selection clutter 0 Answers

Scripting Sprites || URGENT HELP 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