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 vulgarknight · Feb 18, 2013 at 06:36 AM · aifunctionwaypoint

Performing NPC movement with a function.

I am working on a function for a patrolling NPC's, but I've run into some issues. When I had the code in my Update() block, it worked fine, but now it is not updating key variables, and I am sure it is just because I'm not making use of the variables persistently. Here's some code:

 void patrolling(Transform[] waypointAI, int currentWaypointAI, Vector3 targetAI, Vector3 moveDirectionAI, Vector3 velocityAI, float speedAI) {
                 
         wpLengthGUI.text = waypointAI.Length.ToString();
         methodStateGUI.text = "Method Started";
         methodCWP.text = currentWaypointAI.ToString();
         
         
         if (currentWaypointAI < waypointAI.Length) {
             methodStateGUI.text = "Inside first IF";
             
             targetAI = waypointAI[currentWaypointAI].position;
 
             moveDirectionAI = targetAI - transform.position;
             
             velocityAI = rigidbody.velocity;
         } else {
             methodStateGUI.text = "Inside first ELSE";
             
             currentWaypointAI = -1;    
         }
         
         if (moveDirectionAI.magnitude < .1) {
             
             methodStateGUI.text = "Inside second IF";
             currentWaypointAI = currentWaypointAI ++;
             
         } else {
             methodStateGUI.text = "Inside second ELSE";
             velocityAI = moveDirectionAI.normalized * speed;
         }
         //methodStateGUI.text = "Outside IFs, end ";
         rigidbody.velocity = velocityAI;
     }


What happens when I run this as a callable function is that currentWaypointAI is never incremented, even though my NPC is clearly less than .1 units away. Anyone have any insight into this?

Comment
Add comment · Show 2
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 deltamish · Feb 18, 2013 at 06:46 AM 0
Share

Hi, Is this the complete script and are you calling the patrolling function

avatar image vulgarknight · Feb 18, 2013 at 07:37 PM 0
Share

No, this is just the function, the complete script is just this function, but it is called directly in the Update() function. When I use it in an outside function, it does not increment currentWaypoint. If I do this function with individual lines inside Update(), it works fine.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by deltamish · Feb 18, 2013 at 06:48 AM

Hi, Dont use moveDirectionAI.magnitude < .1 This returns higher value as it computes direction and converts it int scalar quantity use Vector3.distance or just change .1 to some higher value(approx 6)

Comment
Add comment · Show 1 · 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 vulgarknight · Feb 18, 2013 at 07:27 PM 0
Share

I tried to increase the value, but my character simply stops when he gets to his first waypoint. I tried with Vector3.Distance, but I get an error: Operator < cannot be applied to operands of type 'method group' and 'int'.

avatar image
0

Answer by Loius · Feb 18, 2013 at 09:45 PM

Functions pass by value (by default). You're changing only your local copy of currentWaypointAI.

You can either pass by ref:

Update() { int argument = 0; Function( ref argument ); Debug.Log(argument); }

Function(ref arg) { arg++; }

Or declare your variable as a class member and don't pass it at all.

Comment
Add comment · Show 3 · 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 vulgarknight · Feb 18, 2013 at 09:49 PM 0
Share

but if i set currentwaypointAI to zero everytime in update, wouldn't the currentwaypointAI always be zero?

avatar image vulgarknight · Feb 18, 2013 at 09:51 PM 0
Share

Also, I eventually want to be able to use my patrolling function in many different AI scripts. To that end, I want to be able to pass it a waypoint array, movement speed, and an NPC to move, and have it work its mighty magic.

avatar image Loius · Feb 19, 2013 at 12:03 AM 0
Share

$$anonymous$$y example was an example; if you run that you'll see that argument is changed inside Function and that it "remains changed" inside Update (it'll print 1, not 0), which is the behaviour you're asking for, ya?

I'd highly recommend making the current waypoint (and much of that other info like speed and friends) a member variable of the NPC class that you pass into the function. If you can say "This NPC has a speed", speed should be a member variable. (And "This NPC has a X" means X should be a member var)

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

12 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

Related Questions

Navmesh Enemy AI, Syntax issue? 1 Answer

WayPoints mixed with Raycast 1 Answer

Way Point Script Change Group 0 Answers

Why isn't a Ai Waypoint Spawning??? 0 Answers

Confused about copying Lists! 1 Answer


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