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 /
avatar image
0
Question by arturoza · Sep 15, 2014 at 10:28 PM · transformaipatrol

AI patrolling script

Hi, I have this script, and I was trying to make it rotate 180 grades once he gets to one patrol point,but I'm lost, I hope you can help me, thanks.

 public class POLICEscript : MonoBehaviour {
     public Transform[] patrol;
     private int Currentpoint;
     public float moveSpeed;
         
     void Start()
         {
             transform.position = patrol [0].position;
             Currentpoint = 0;
         }
     void Update()
         {
             if(transform.position == patrol[Currentpoint].position)
                 {
                     Currentpoint++;
                 }
 
             if(Currentpoint >= patrol.Length)
                 {
                     Currentpoint = 0;
                 }
 
             transform.position = Vector3.MoveTowards (transform.position, patrol [Currentpoint].position, moveSpeed * Time.deltaTime);            
         }
 
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 Cherno · Sep 15, 2014 at 10:51 PM

First of all, I would suggest using a Vector3[] as your patrol coordinates, no reason to use a transform, is there? Anyway, since an object's position will almost certainly never be exactly the same as another one's if it hasn't been moved there by setting the values by hand, you will be better off checking the Vector3.distance between the character and his next patrol position. Once he gets within a certain distance, like 0.5f units, he has has more or less reached it and can continue on to the next. The threshold could of course be made much smaller, it's worth experimenting. Just don't make it too small (0.00xxx) or it will never be reached.

You just have to modify the line like this:

   if(Vector3.Distance(transform.position, patrol[Currentpoint]position) < 0.5f) {
         Currentpoint++;
   }

As for rotating, there are several ways to do it, one is explained here : http://blog.anthonybaker.me/2010/09/rotating-your-character-in-unity3d.html

Comment
Add comment · Show 4 · 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 arturoza · Sep 15, 2014 at 11:51 PM 0
Share

thank you so much Cherno, but I'm really stuck with the rotating thing. I hope you could help me with this

avatar image Cherno · Sep 16, 2014 at 12:32 AM 0
Share
   public float rotateSpeed = 2.0f;

 private bool rotating;

 void Update() {
     StartCoroutine(TurnTowards(-transform.forward));
 }

 IEnumerator TurnTowards(Vector3 lookAtTarget) {    

     if(rotating == false) {
         //Vector3 direction = lookAtTarget - transform.position;//this can be deleted, it's never used :P
         Quaternion newRotation = Quaternion.LookRotation(lookAtTarget - transform.position);
         newRotation.x = 0;
         newRotation.z = 0;

         for (float u = 0.0f; u <= 1.0f; u += Time.deltaTime * rotateSpeed) {
             rotating = true;
             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, u);

             yield return null;
         }
         rotating = false;
     }
 }
avatar image arturoza · Sep 16, 2014 at 01:27 AM 0
Share

Thank you so much Cherno!!!!!

avatar image Cherno · Sep 16, 2014 at 01:35 AM 0
Share

You are welcome. I just noticed that I left the direction variable in the CoRoutine by mistake, it's never used so I will edit my post to reflect that.

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

24 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

Related Questions

How to remove z axis control from script 1 Answer

Patrol system not working? Ignoring points(?) 0 Answers

npc patrol and chase 1 Answer

problem with my script need help 2 Answers

Multiple objects follow the object in front 4 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