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.
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.
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;
}
}
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.
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.
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.
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
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