- Home /
Make a character walk around randomly
I have want to make a game object move around randomly, like a person. I do not need information on how to animate the character (run, walk, jump) but i just want it to walk around the map aimlessly. Thanks
**ps i just want s script that i can copy and past directly into monddevelope without any complier errors Thanks
You could choose a random point on the terrain every once in a while, e.g. every 5 seconds, and then just have the character walk towards that.
Answer by Tomer-Barkan · Oct 10, 2013 at 08:08 AM
I did that once, where the character simply chooses a direction, then moves a few seconds in that direction. Then chooses another direction, and so on.
You can tweak this as much as you want, so add a random element to the amount of time he walks in each direction.
Sample code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NPCController : MonoBehaviour {
private float timeToChangeDirection;
// Use this for initialization
public void Start () {
ChangeDirection();
}
// Update is called once per frame
public void Update () {
timeToChangeDirection -= Time.deltaTime;
if (timeToChangeDirection <= 0) {
ChangeDirection();
}
rigidbody.velocity = transform.up * 2;
}
private void ChangeDirection() {
float angle = Random.Range(0f, 360f);
Quaternion quat = Quaternion.AngleAxis(angle, Vector3.forward);
Vector3 newUp = quat * Vector3.up;
newUp.z = 0;
newUp.Normalize();
transform.up = newUp;
timeToChangeDirection = 1.5f;
}
}
@$$anonymous$$er-Barkan can you send this script with solutions? i mean, when i read "float angle = Random.Range(0f, 360f);" that with // is typed "this float made that ..."
thanks,
Answer by Ota_TB · Oct 10, 2013 at 08:16 AM
http://answers.unity3d.com/questions/57067/make-objects-walk-around-randomly.html
This should be what your asking for.