Does anybody know of a script for a character to wander around?
And does anybody know of a script where the character slowly walks toward another gameobject.
P.S i tried unity steer and that just confused me.
Does anybody know of a script for a character to wander around?
And does anybody know of a script where the character slowly walks toward another gameobject.
P.S i tried unity steer and that just confused me.
Although there are various ways either of those behaviors could be implemented, UnitySteer would be a straightforward way to accomplish both things. (Or at least I assume so - I haven’t used it myself, but assuming it implements the most common steering behaviors, ‘wandering’ and moving towards a target should both be covered.)
If you’re having trouble with UnitySteer, you could always post whatever questions you have about it here.
For the script that the character walks toward another object, why not just make a smooth look at (game Object) and a transform translate (for an object)?
Also, you can add a ray cast, so when the character is near, but not together to the object, stop the translation.
And also, there is a made function in a Unity FPS example (See target), that allow you activate the translation only if the object is visible.
I did it time ago in this way and it works, I’ll look for it if I can.
What you are asking for is basically handled a few different ways motbest suggested using translation. I suggest using physics and velocities. but both are on the same principle. use the Quaternion.LookRotation() to get the angle that you want you can then simply point your character in that direction and move him. It is a touch more complicated but that is the basics.
There are 2 basic steps that you need. Move and AreWeThereYet. quite simple. I have built a simple move function already which I can share.
These functions are the basis of many different forms of AI movement. Move to target, once you get there go find another place to move. Every frame, set your target to the location of your enemy, and always move towards him. Set your target to the opposite direction of your enemy, always move away. Follow a cycling waypoint (racing). Many, many reasons to use this type of code.
function Update(){
if(distanceToTarget()<5.0){
return true;
}
doMove();
}
//do move
function doMove(){
var relativePos=target.position - transform.position;
var lookAt= Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Lerp (transform.rotation, lookAt, Time.deltaTime * 10);
rigidbody.velocity=transform.forward * walkSpeed;
}
function LateUpdate(){
if(grounded){
transform.localEulerAngles.x=0;
transform.localEulerAngles.z=0;
}
}
function distanceToTarget(){
return Vector3.Distance(target.position, transform.position);
}
I do some basic wandering using Random.insideUnitCircle, it works pretty well. If you multiply it by a radius modifier it will return a random vector2 position on a circle around the object you cast it from, so you can just make a vector3 out of it using the object’s transform.position y value, then move to it.
var wanderRadius : float = 5.0;
private var destination : Vector3 = Vector3.zero;
function ChooseRandomDestination(){
var randomCirclePoint : Vector2 = Random.insideUnitCircle * wanderRadius;
destination = new Vector3(randomCirclePoint.x, transform.position.y, randomCirclePoint.z);
}
I thought I would mention that there is a great SeekSteer script over at the Unity Community Wiki. It is much much easier to understand than UnitySteer…and I would assume much more light weight for CPU if you are making a mobile game.
http://www.unifycommunity.com/wiki/index.php?title=SeekSteer
What we really need is a translation of the ChaseAndEvade sample that Microsoft provides to XNA developers. It includes, Chase, Wander and Evade algorithms and is very easy to understand and implement.
http://create.msdn.com/en-US/education/catalog/sample/chase_evade
Allan