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 /
  • Help Room /
avatar image
0
Question by dead_byte_dawn · Nov 06, 2016 at 11:41 PM · c#enemy aipatrol

Enemy AI patrol mode question.

I have a simple AI script here that chases a hero whenever it detects a collision on its LEFT or RIGHT Raycast. What doesn't seem to be working ATM is the XOR "^" function in FixedUpdate(). It only Logs when the player is being chased which, is the opposite of what its supposed to do. Am I using XOR correctly, or am I missing something bigger?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class enemyAi : MonoBehaviour {
 
     public float enemyMoveSpeed = 1f;
     private bool heroDetectedLeft = false;
     private bool heroDetectedRight = false;
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.gameObject.CompareTag ("Players")) {
             Debug.Log ("Collision detected");
             other.gameObject.transform.position = Vector2.up;
             // TODO: Hero loses 1 HP.
             // TODO: Hero drops X Coins.
         }
     }
 
     void PatrolMode(){
             // TODO: Enemy patrol between platform edges.
             // TODO: Setup GroundCheck detection.
     }
 
     void GroundCheck(){
 //        RaycastHit2D floor = Physics2D.Raycast (transform.position, Vector2.down, 1f);
 
     }
 
     void LookLeft(){
         RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.left,4f);
         if (hit.collider != null) {
             Debug.Log("Ray hit: "+hit.collider.gameObject.name);
 
             if (hit.collider.tag=="Players") {
                 Debug.Log ("KILL KILL");
                 heroDetectedLeft = true;
             }
         }
         else{
             heroDetectedLeft = false;
         }
     }
 
 
     void LookRight(){
         RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right,4f);
         if (hit.collider != null) {
             Debug.Log ("Ray hit: " + hit.collider.gameObject.name);
 
             if (hit.collider.tag == "Players") {
                 Debug.Log ("KILL KILL");
                 heroDetectedRight = true;
             }
         } else {
             heroDetectedRight = false;
         }
 
     }
 
 
     void FixedUpdate()
     {
         LookLeft ();
         LookRight ();
         if (heroDetectedLeft ^ heroDetectedRight) {
             Debug.Log("Resuming = PatrolMode");
         }
         if (heroDetectedLeft == true) {
             transform.Translate (Vector2.left * enemyMoveSpeed * Time.deltaTime); 
         } 
         if (heroDetectedRight == true) {
             transform.Translate (Vector2.right * enemyMoveSpeed * Time.deltaTime);
         }else {
             this.transform.Translate (Vector2.zero);
 
         }
 
     }
 
 
 }
 
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
Best Answer

Answer by Zynek · Nov 07, 2016 at 09:31 AM

If you have doubts about XOR, you can use OR just as well. I dont see any kind of time out on detection, where you would set both heroDetected booleans to false, this way your AI could kinda runaway from your game.

Thats about what i can figure out of what you provided. Maybe post some pictures ? Or specify the problem in more details.

Comment
Add comment · Show 2 · 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 dead_byte_dawn · Nov 08, 2016 at 12:52 AM 0
Share

Thanks for the input Zynek, I'm thinking of just adding a Coroutine that checks if both left and right bools are false then trigger a "Patrol" function. For some reason I can't get the XOR bitwise to do what I'm asking.

avatar image Zynek dead_byte_dawn · Nov 08, 2016 at 08:32 PM 1
Share

Oooooh but XOR is exclusive OR - meaning it triggers on odd amount of true. So if i get it right, you´re looking for:

 if (heroDetectedLeft && heroDetectedRight) {
           // Patrol $$anonymous$$ode
 }else if (heroDetectedLeft == true) {
           //move Left
 } else if (heroDetectedRight == true) {
           //move Right
          }

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

257 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

Related Questions

If statement not working in script. 3 Answers

Will this work? 0 Answers

how to make an enemy patrol and chase me 0 Answers

Enemy AI. How to make the AI chase the player by using the exact same moves as the player. (2D) 1 Answer

Trying to add a tracker to enemy AI either prefab or with reference 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