• 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 Sigma616 · May 31, 2012 at 10:58 PM · c#movementaipathing

Pathing & Patrol AI Error

Hello, my team and I have it a wall in regards to our code that governs the patrol and hunting of our Enemy AI controlled objects in our game. The Code, in terms of hunting and chasing, works perfectly, The issue lies in the patrol portion itself. The Object for some reason always wants to move to a certain location on the Z axis, as far as I can tell, I am not defining this. Any suggestions would be greatly appreciated, this is just a snippet of the file, if requested I can post the whole thing, but this is where the patrol issue is occurring.

 else if(distance > 20.0)//Patrol Range
     {

         Debug.LogWarning("Lost Target, returning to patrol");
     
         //Look for Target, Where are the savings... Savings!
         patrolAmount.x = patrolInc * ((float)moveSpeed * 2f) * Time.deltaTime;
 
         if (patrolInc > 0.0f && transform.position.x >= patrolRangePos)
         {
             patrolInc = -1.0f;
         }
         else if (patrolInc < 0.0f && transform.position.x <= patrolRangeNeg)
         {
                 patrolInc = 1.0f;
         }
         transform.Translate(patrolAmount);
     }
Comment
Add comment · Show 1
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 Chimera3D · May 31, 2012 at 11:24 PM 0
Share

We'll need the whole thing :/

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sigma616 · Jun 01, 2012 at 12:38 AM

Here is the full Code as requested:

     using UnityEngine;
     using System.Collections;
     
     public class EnemyAI : MonoBehaviour 
     {
         public Transform target;//This is what the AI looks for, the Target must  be tagged "Player"
         public int moveSpeed;// This is a base value of how fast the AI moves
         public int rotationSpeed;// This is a Base Value of how fast the AI rotates
         public int powerWalkMultiplier = 2;//The Multiplier for Power Walking
         public int runSpeedMultiplier = 4;//The Multiplier for Running
         public float patrolRangePos = 0.0f;//How far does the AI patrol
         public float patrolRangeNeg = 0.0f;//^
         private float patrolInc = 0.25f;// increment of the Patrol
         private Vector3 patrolAmount;
         private Transform myTransform;
         
         void Awake()
         {
             myTransform = transform;//Keeps it Active
         }
         
         // Use this for initialization
         void Start () 
         {
             GameObject go = GameObject.FindGameObjectWithTag("Player");
             
             target = go.transform;
             
         }
         
         // Update is called once per frame
         void Update () 
         {
             Debug.DrawLine(target.position, myTransform.position, Color.green);//Plots course
     
             float distance = Vector3.Distance (this.transform.position, GameObject.FindGameObjectWithTag("Player").transform.position);//Calculate Distance
             
             if(distance < 10.0)//Chase Range
             {
                 //Debug.LogWarning("Enemy Sighted! Pursuing!!!");
                 Debug.DrawLine(target.position, myTransform.position, Color.red);
                 //Look At Target, Oh look! Target!
                 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
                 //This takes the Cube's transform via a Slerp, it's a rotation based on the angle it's loking at 
     
             
                 //Move to Target, Hooray Savings!
                 myTransform.position += myTransform.forward * (moveSpeed * runSpeedMultiplier) * Time.deltaTime;//Trigger Run Speed
             }//End Chase Range
             else if(distance < 20.0)//Investigate Range
             {
                 //Debug.LogWarning("Enemy Detected, Investigating...");
                 Debug.DrawLine(target.position, myTransform.position, Color.yellow);
                 //Look At Target, Oh look! Target!
                 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
                 //This takes the Cube's transform via a Slerp, it's a rotation based on the angle it's loking at 
                 
             
                 //Move to Target, Hooray Savings!
                 myTransform.position += myTransform.forward * (moveSpeed * powerWalkMultiplier) * Time.deltaTime;//Trigger power walk Speed
             }//End Investigate Range
             else if(distance > 20.0)//Patrol Range
             {
     
                 //Debug.LogWarning("Lost Target, returning to patrol");
             
                 //Look for Target, Where are the savings... Savings!
                 patrolAmount.x = patrolInc * ((float)moveSpeed * 2f) * Time.deltaTime;
         
                 if (patrolInc > 0.0f && transform.position.x >= patrolRangePos)
                 {
                     patrolInc = -1.0f;
                 }
                 else if (patrolInc < 0.0f && transform.position.x <= patrolRangeNeg)
                 {
                         patrolInc = 1.0f;
                 }
                 transform.Translate(patrolAmount);
             }
             
         }
         
     }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Random movement AI 0 Answers

Problems when i have multiple enemies on the same scene C# 1 Answer

Player movement issue... 1 Answer

Help with AI follow range 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