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 /
This question was closed Apr 11, 2017 at 01:29 PM by CosmicSloth for the following reason:

Other

avatar image
0
Question by CosmicSloth · Apr 04, 2017 at 10:33 AM · javascriptaicar gamepatrol

Editing an AI patrolling script

I found an AI patrolling script (JS) that uses GameObjects as waypoints for a car game I am making for a project at college. The script is exactly what I wanted except for the fact that it has some code in that makes the waypoints always at the characters height. As my game has different levels and ramps, as soon as the car goes of the end of the ramp it just then patrols the points at the same height rather than coming back down. How can I change the code to accommodate this? It's under function patrol. I have tried taking the 3 lines out but I get a compiler error and I tried changing the axis to no avail.

 #pragma strict
  
  var waypoint : Transform[];            // The amount of Waypoint you want
  var patrolSpeed : float = 3;        // The walking speed between Waypoints
  var loop : boolean = true;            // Do you want to keep repeating the Waypoints
  var dampingLook = 6.0;                // How slowly to turn
  var pauseDuration : float = 0;        // How long to pause at a Waypoint
  
  private var curTime : float;
  private var currentWaypoint : int = 0;
  private var character : CharacterController;
  
  function Start(){
  
      character = GetComponent(CharacterController);
  }
  
  function Update(){
  
      if(currentWaypoint < waypoint.length){
          patrol();
          }else{    
      if(loop){
          currentWaypoint=0;
          } 
      }
  }
  
  function patrol(){
  
          var target : Vector3 = waypoint[currentWaypoint].position;
          target.y = transform.position.y; // Keep waypoint at character's height
          var moveDirection : Vector3 = target - transform.position;
  
      if(moveDirection.magnitude < 0.5){
          if (curTime == 0)
              curTime = Time.time; // Pause over the Waypoint
          if ((Time.time - curTime) >= pauseDuration){
              currentWaypoint++;
              curTime = 0;
          }
      }else{        
          var rotation = Quaternion.LookRotation(target - transform.position);
          transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
          character.Move(moveDirection.normalized * patrolSpeed * 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

  • Sort: 
avatar image
1
Best Answer

Answer by VeeooshL · Apr 04, 2017 at 11:10 AM

I think that removing the line target.y = transform.position.y (line 32) should work though I cannot test it at the moment.

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 CosmicSloth · Apr 04, 2017 at 11:37 AM 0
Share

Yeah I removed it and it seems to be working now, such a simple fix as well, thanks a bunch

avatar image VeeooshL CosmicSloth · Apr 04, 2017 at 11:39 AM 0
Share

happy to help :)

Follow this Question

Answers Answers and Comments

165 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

Related Questions

AI Patrol 0 Answers

How do I get my AI to chase the player?,How do I go from patrolling to chasing the player? 3 Answers

make AI follow random waypoints 0 Answers

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

Patrol and Target Player Code? 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