• 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 awsome129 · Jan 16, 2017 at 07:45 AM · airandompathfindingrandom.rangepatrol

My question is almost fully answered(AI help)

I asked before about my ai script I had and got improvements. Those improvements were fine for the time being, but now i want to move forward with my AI and this is preventing me from doing so. The answer I got when i first asked about having my ai randomly patrol was to use Vector3.MoveTowards, I was a little skeptical because I remember reading that this only moves in a straight line. But i used it and it worked a little bit. But now when my ai character gets to the point it wanted to go, it moves(thankfully it does not teleport) to a bunch of other points about 5 feet from the original point. How do I make my ai take some time when it gets to a destination, and then move like 30 feet from the new point it has reached. Help please

My script:

 using UnityEngine;
 using System.Collections;
 
 public class AIBehaviour : MonoBehaviour
 {
 
   
     public Transform player;
     bool PlayerSeen;
     float movespeed;
     float timer = 5;
     public int moveCount;
     public bool AImove;
     Animator anim;
     float AIhealth;
     Vector3 currentTransformedPos;
 
 
 
     // Use this for initialization
     void Start()
     {
         PlayerSeen = false;
         movespeed = .08f;
         moveCount = 0;
         AImove = false;
         anim = GetComponentInChildren<Animator>();
         AIhealth = 100;
 
     }
     // Update is called once per frame
     void Update()
     {
       
 
         timer -= Time.deltaTime;
 
         Vector3 direction = player.position - this.transform.position;
         float angle = Vector3.Angle(direction, this.transform.forward);
         float angleb = -Vector3.Angle(direction, this.transform.forward);
 
 
 
         if (PlayerSeen == false)
         {
             transform.LookAt(new Vector3(Random.Range(10, 30), 0, Random.Range(10, 30)));
             transform.position = Vector3.MoveTowards(transform.position, new Vector3(Random.Range(10,30), 0, 0), movespeed);
             moveCount = 1;
             currentTransformedPos = transform.position;
         }
 
         if (currentTransformedPos == transform.position && (PlayerSeen == false))
         {
             transform.LookAt(new Vector3(Random.Range(10, 30), 0, Random.Range(10, 30)));
             transform.position = Vector3.MoveTowards(transform.position, new Vector3(Random.Range(10, 30), 0, 0), movespeed);
             moveCount = 1;
             currentTransformedPos = transform.position;
         }
 
 
         //Player Position
         if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
         {
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                                         Quaternion.LookRotation(direction), 0.1f);
         }
 
 
         if (direction.magnitude > 5)
         {
             this.transform.Translate(0, 0, 0.3f);
         }
 
 
 
         if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 30 || (Vector3.Distance(player.position, this.transform.position) < 4 && angleb < 0))
         {
             PlayerSeen = true;
         }
 
 
         if (Vector3.Distance(player.position, this.transform.position) > 10 || (Vector3.Distance(player.position, this.transform.position) > 4))
         {
             PlayerSeen = false;
         }
 
         { 
             if (moveCount == 1)
             {
                 AImove = true;
             }
 
             if (moveCount == 0)
             {
                 AImove = 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NavMesh.SamplePosition returns point outside the NavMesh 0 Answers

Racing Ai? long read 1 Answer

How can I make AI follow waypoints and choose random ones? 1 Answer

AI teleporting 1 Answer

How do I get my patrolling enemies to go back to the patrol point they had not gotten to because they chased the player? 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