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 Menatombo · Mar 07, 2014 at 11:17 PM · javascriptaipatrolwander

Need to walk idly around unless ball is around

I've been struggling with this one for days now. I think I might be close, but I can't figure it out for the life of me. I need for the AI to wander around aimlessly (which it does) unless a ball is thrown on to the playing field. Then I need for it to try to get the ball. I have the getting the ball part and it works good. I have the wandering AI part which also works great. What I need help on is combining the two. I've tried if statements, but keep getting errors.

Here's what I have so far.

#pragma strict @script RequireComponent(CharacterController) var speed:float = 5; var directionChangeInterval:float = 1; var maxHeadingChange:float = 30; var heading: float=0; var targetRotation: Vector3 ; var Ball: GameObject; var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var myTransform : Transform; /// /// Creates wandering behaviour for a CharacterController. /// function Awake (){ myTransform = transform; // Set random initial rotation transform.eulerAngles = Vector3(0, Random.Range(0,360), 0); // look in a random direction at start of frame. //StartCoroutine NewHeadingRoutine (); } function Start() { var go = GameObject.FindWithTag("Ball"); } function Update (){ var go = GameObject.FindWithTag("Ball"); if (go == null) return; target = go.transform; myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; var controller : CharacterController = GetComponent(CharacterController); transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval); var forward = transform.TransformDirection(Vector3.forward); controller.SimpleMove(forward * speed); } /// <summary> /// Repeatedly calculates a new direction to move towards. /// Use this instead of MonoBehaviour.InvokeRepeating so that the interval can be changed at runtime. /// </summary> while (true){ NewHeadingRoutine(); yield WaitForSeconds(directionChangeInterval); } /// <summary> /// Calculates a new direction to move towards. /// </summary> function NewHeadingRoutine (){ var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360); var ceil = Mathf.Clamp(heading + maxHeadingChange, 0, 360); heading = Random.Range(floor, ceil); targetRotation = new Vector3(0, heading, 0); }

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 morethan3 · Mar 08, 2014 at 02:23 AM 0
Share

Are the erros occur on these lines:

var go = GameObject.FindWithTag("Ball"); if (go == null) return;

avatar image Menatombo · Mar 08, 2014 at 02:58 AM 0
Share

I actually don't get any errors. If I take out my ball code it will move around the scene just fine. If I use only my ball code it will go directly to the ball. I've been at a loss trying to get it to do both. Check the scene for a ball and if there is a ball to go to it. If not... Then mill around the scene.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KellyThomas · Mar 08, 2014 at 03:55 AM

It looks like you want your AI to be in one of several states i.e. either idle or seek but never both at the same time. This is a scenario that can be modeled with a Finite State Machine (FSM). These can be quite complex, or quite simple.

This is one implementation that uses an enum/switch based FSM:

 enum AIState { Idle, Seek }
 
 var state: AIState = AIState.Idle;
 var target: Transform; 
 
 function Update() {
     switch(state) {
         case AIState.Idle:
             UpdateIdle();
             CheckIdleTransitions();
             break;
         case AIState.Seek:
             UpdateSeek();
             CheckSeekTransitions();
             break;
     }
 }
 
 function UpdateIdle() {
     //perform idle actions
     // ...
 }
 
 function CheckIdleTransitions() {
     //check exit conditions
     var go = GameObject.FindWithTag("Ball");
     if (go != null) {
         target = go.transform;
         state = AIState.Seek;
     }
 }
 
 function UpdateSeek() {
     //perform seeking actions
     // ...
 }
 
 function CheckSeekTransitions() {
    //check exit conditions
    // ...
 }


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 Menatombo · Mar 08, 2014 at 04:48 AM 0
Share

Thank you so much! I wasn't sure how to do if else in Unityscript, but a state machine was the perfect solution! (Sometimes I'm just so close to the problem that I don't think of the right solutions!)

Thanks again! It works swim$$anonymous$$gly!

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

22 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

Related Questions

Enemy following Player in range 2 Answers

Help with Patrol Script!! 1 Answer

AI teleporting 1 Answer

A* optimization and path help 0 Answers

AI will complete patrolling and wait for 5 seconds 2 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