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 /
  • Help Room /
avatar image
0
Question by JeromyB · May 19, 2015 at 10:11 AM · ai problems

I need help making a free roaming script for animals.

I'm starting work on my first game and have the character walking around and was able to make some nice terrain with trees and fixed a broken skybox. But I can't find anything online about how to make animals, creatures, or npc for that matter to walk around the world with AI. I could really use the help, maybe even if I could get a team together who could help with things like this. I had a friend who made games but he talks everyone down and just laughed at me the last time I ran into problems. So I need help from someone new who I can actually work with. Also I am pretty new at this and I'm still learning A LOT. So any help would be appreciated.

Comment
Add comment · Show 1
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 Owen-Reynolds · May 19, 2015 at 02:21 PM 0
Share

There used to be a lot of people here working on Wandering Zombie scripts. If you delete the part that attacks the player, wandering zombies and wandering bunnies are about the same. $$anonymous$$any do something similar to Cherno's, with other stuff thrown in.

But, really, the best help is to read any basic book / website on learning C#. You'll need to know it to adjust any scripts you find. Or just quickly skim stuff for ideas and write your own. And you'll suddenly be able to search better "how to program X," and understand the answers a lot more.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cherno · May 19, 2015 at 11:14 AM

I suggest you start by creating a simple script that periodically determines a random Vector3 position in a certain radius around the animal. This will be it's new waypoint. Then, you call a function that makes the animal go there by changing its transform.position using Vector3.Lerp or .Slerp.

After you have understood how to do this, you can tackle the fairly complex tpic of pathfinding; I suggest using the Astar Pathfinding Project, available for free on the asset store.

 using UnityEngine;
 using System.Collections;
 
 public class Test: MonoBehaviour {
     public Vector3 targetPos;
     public bool isMoving = false;
     public float maxRange = 10f;
     public float waitTime = 3f;
     public float speed = 0.05f;
     
     void Update() {
         if(isMoving == false) {
             FindNewTargetPos();
         }
     }
     
     private void FindNewTargetPos() {
         Vector3 pos = transform.position;
         targetPos = new Vector3();
         targetPos.x  = Random.Range(pos.x - maxRange, pos.x + maxRange);
         targetPos.y = pos.y;
         targetPos.z = Random.Range(pos.z - maxRange, pos.z + maxRange);
 
         transform.LookAt(targetPos);
         StartCoroutine(Move());
     }
     
     IEnumerator Move() {
         isMoving = true;
         
         for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * speed) {
 
             transform.position = Vector3.MoveTowards(transform.position, targetPos, t);
             yield return null;
         }
         
         yield return new WaitForSeconds(waitTime);
         isMoving = false;
         yield return null;
     }
     
     
     
 }
 
 
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 Owen-Reynolds · May 19, 2015 at 02:28 PM 0
Share

I think line 33 is supposed to be a Lerp (since t goes from 0 to 1.) Otherwise, in $$anonymous$$oveTowards, t is meters per frame, which will look like a magnet is sucking up the poor bunny.

avatar image Cherno · May 19, 2015 at 03:38 PM 0
Share

No, it would workd with Lerp as well, but then you have the problem that it always moves at a speed relateive to the distance, which we don't want; We need to move at a constant speed, which is why Vector3.$$anonymous$$oveTowards is used.

avatar image Owen-Reynolds · May 19, 2015 at 04:07 PM 0
Share

But test it, and look at other examples using $$anonymous$$oveTowards. The math is funny.

Lerp-move uses a 0-1 loop, since you're always done at percent=1. $$anonymous$$oveTowards uses either a "while I'm not there" loop, or precomputes the time needed. In this case, $$anonymous$$oveTowards probably isn't done when t=1. It might be done sooner, or later (in which case it just sits and waits.)

The 3rd input of $$anonymous$$oveTowards is the speed. Usually a constant speedIn$$anonymous$$pS*Time.deltaTime. It can change as you move, but looks best if it quickly goes to max speed, then slows just as you are about to reach the target.

avatar image JeromyB · May 23, 2015 at 10:31 AM 0
Share

It had a bunch of compile errors saying that it was expecting other things and I can't fix it. Everything seems fine, Unity is just giving wrongful errors... I think

avatar image
0

Answer by g7dme · Jul 04, 2018 at 05:51 PM

hi i had a very similar problem myself recently and as my coding is worse than beginner i had to find a new way to generate the effect i wanted after much head scratching i came up with a very simple solution that works extremely well. take any AI animal or NPC that has a follow object holder in there AI script then set them down on a flat plain for the initial setup (note the animal is simply used as a reference point and test object to see if the correct effect is achieved i chose the largest animal i was using in my basic world a moose to create the free roam prefab). Start by seting up 8 way points around the animal (N NE E SE S SW W NW) the size of the circle will have to be found by trail and error to create the right effect mark the way points around the AI object in the normal way then send the animal to the first check point and then let it follow the way points to check its working properly. The next task is again trial and error adjust the way point ids around the circle to create the illusion of random wondering, when you are satisfied with the effect remove the animal from the centre of the way points. Create the new prefab i called mine free roam attach the new prefab to any AI object and test it in a normal AI environment if you have set up correctly the NPC object should appear to freely roam around your environment. For those interested the way it works it is very simple when the AI character moves towards each new target it takes the whole ring of way points with it as the NPC always moves to the last known location of its target it effectively generates a new random location for its next target thus creating the illusion of roaming free.

of coarse you can refine its wondering by using more way point but i found 8 more than adequate for most situations.

De G7DME

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Script For Animal AI 1 Answer

hi how can you get rid of this ? error CS1501: No overload for method `Instantiate' takes `2' arguments 1 Answer

Disable a Function when another and active (javascript AI) 1 Answer

Zombie Horde All Walking in Line! I Need them to spread out. 4 Answers

2D How do i choose one out of two or three colliding gameobjects of the same type? 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