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
2
Question by Alien · Oct 26, 2011 at 10:43 PM · javascriptpathfindingmovewalk

About AI moving randomly or use A* Pathfinding

Hi,I am also new in Unity.

I have a question about the character moving randomly.

For example,

an enemy walking in the game and I need it to walk on some path or randomly

I do not know if I would like to get this goal, I have to get some Scripts or use A* Pathfinding.

Chould show me a script keyword or some resources of A* Pathfinding?

Thanks a lot.

And sorry for my poor English.

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
2

Answer by ocularcash · Oct 27, 2011 at 03:16 AM

There is resources on this site for A* Pathfinding, just have a look around the site and you'll find a couple resources on it. You can also find alot of pathfinding techniques on youtube. Wandering AI's are a little tricky tho because it can take a long time to write a complex wandering AI script that works good for the environment in your game, Especially if it's an open world where it's wandering. I'll put a very simple free roaming AI script down but remember it's very basic. You'll have to adjust where needed a add a few things depending on your environment. I got this script from steamisM50 but converted it from c# to javascript and taking out some of the stuff you won't need. He has quite a few tutorials on AI so I suggest having a look at his videos.

Wandering AI Script:

 var moveSpeed : float = 2;
 var rotSpeed : float = 10;
 var moveDir : int = 1;
 
 function Update()
 {
     if(!Physics.Raycast(transform.position, transform.forward, 5))
     {
         transform.Translate(Vector3.forward * moveSpeed * Time.smoothDeltaTime);
     }
     else
     {
         if(Physics.Raycast(transform.position, - transform.right, 1))
         {
             moveDir = 1;
         }
         else if(Physics.Raycast(transform.position, transform.right, 1))
         {
             moveDir = -1;
         }
         transform.Rotate(Vector3.up, 90 * rotSpeed * Time.smoothDeltaTime * moveDir);
     }
 }

WayPoint Script:

 var waypoints : Transform[];
 var speed : int = 2;
 private var currentWaypoint : int;
 var loop : boolean = true;
 var player : Transform;
 
 function Start()
 {
     player = GameObject.FindWithTag("Player").transform;
 }
 
 function Update()
 {
     if(currentWaypoint < waypoints.length)
     {
         
         var target : Vector3 = waypoints[currentWaypoint].position;
         var moveDirection : Vector3 = target - transform.position;
         var range : Vector3 = player.position - transform.position;
         
         var velocity = rigidbody.velocity;
         if(moveDirection.magnitude < 1)
         {
             currentWaypoint++;
         }
         else if(range.magnitude < 10)
         {
             
             velocity = Vector3.zero;
             target = player.position;
         }
         else
         {
             velocity = moveDirection.normalized * speed;
         }
     }
     else
     {
         if(loop)
         {
             currentWaypoint = 0;
         }
         else
         {
             velocity = Vector3.zero;
         }
     }
     rigidbody.velocity = velocity;
     transform.LookAt(target);
 }

To add the WayPoint script just make as many empty game objects as you need and name them Waypoint1 etc... attach the script to the AI and drag and drop the waypoints.

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 Alien · Oct 30, 2011 at 08:41 PM 0
Share

Thanks a lot! I'll try it again.:)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make enemy run animation 3 Answers

collider then change rotation converse? 0 Answers

Mouse as a throttle 1 Answer

move childA to position of childB 1 Answer

Why don't my enemies walk toward me? 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