- Home /
Random movement, staying in an area.
Hey I have acquired a script that allows for random movement, the problem is my object just wanders off the plane and ignores collision. Is there anyway I can set a boundary for this random movement to stop it venturing off the plane and into dead space? Thanks, here's what I have so far:
var chicken : Transform;
var vel : Vector3;
var Direction : float = 3;
var curTime : float = 0;
function Start()
{
SetVel();
}
function SetVel()
{
if (Random.value > .5) {
vel.x = 10 * 10 * Random.value;
}
else {
vel.x = -10 * 10 * Random.value;
}
if (Random.value > .5) {
vel.z = 10 * 10 * Random.value;
}
else {
vel.z = -10 * 10 * Random.value;
}
}
function Update()
{
if (curTime < Direction) {
curTime += 1 * Time.deltaTime;
}
else {
SetVel();
if (Random.value > .5) {
Direction += Random.value;
}
else {
Direction -= Random.value;
}
if (Direction < 1) {
Direction = 1 + Random.value;
}
curTime = 0;
}
}
function FixedUpdate()
{
chicken.rigidbody.velocity = vel;
}
Any help is appreciated :)
Answer by Rosonator · Oct 23, 2012 at 11:41 AM
This is what I did 2 hours ago. It allows a random movement throught a surface which limits I have established manually. It sets the correct rotation of the object too, but be careful with the correction of 90º degrees I had to do and apply yours. I hope this helps to fix your code.
public float velocidadMax;
public float xMax;
public float zMax;
public float xMin;
public float zMin;
private float x;
private float z;
private float tiempo;
private float angulo;
// Use this for initialization
void Start () {
x = Random.Range(-velocidadMax, velocidadMax);
z = Random.Range(-velocidadMax, velocidadMax);
angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
transform.localRotation = Quaternion.Euler( 0, angulo, 0);
}
// Update is called once per frame
void Update () {
tiempo += Time.deltaTime;
if (transform.localPosition.x > xMax) {
x = Random.Range(-velocidadMax, 0.0f);
angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
transform.localRotation = Quaternion.Euler(0, angulo, 0);
tiempo = 0.0f;
}
if (transform.localPosition.x < xMin) {
x = Random.Range(0.0f, velocidadMax);
angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
transform.localRotation = Quaternion.Euler(0, angulo, 0);
tiempo = 0.0f;
}
if (transform.localPosition.z > zMax) {
z = Random.Range(-velocidadMax, 0.0f);
angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
transform.localRotation = Quaternion.Euler(0, angulo, 0);
tiempo = 0.0f;
}
if (transform.localPosition.z < zMin) {
z = Random.Range(0.0f, velocidadMax);
angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
transform.localRotation = Quaternion.Euler(0, angulo, 0);
tiempo = 0.0f;
}
if (tiempo > 1.0f) {
x = Random.Range(-velocidadMax, velocidadMax);
z = Random.Range(-velocidadMax, velocidadMax);
angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
transform.localRotation = Quaternion.Euler(0, angulo, 0);
tiempo = 0.0f;
}
transform.localPosition = new Vector3(transform.localPosition.x + x, transform.localPosition.y, transform.localPosition.z + z);
}
Well, you can use what you want, but this script works and you won't have any problem combining a C# script with other JS scripts, because that's what UNity does great: abstract from the code. And if by any case you still need to add this feature to a JS script sure that you can understand that C# code and translate what you need to JS by your own, right?
I've tried your c# code the object can move only one time then no move. I also log the variable tiempo this variable is always zero every update. I also changed from tiempo += Time.deltaTime; to tiempo += 2; but nothing gonna happen tiempo still zero. What shall I do?
@Nercoe Sorry but you are asking too much. You cant find lots of people answering with an exact code. At least a thanks would keep this community on its feet.
@Rosonator Thanks a lot. This was what i was looking for.
@Rosonator Thanks for Sharing.. how to control the speed..??
@coolvin In this script decrease your public float velocidad$$anonymous$$ax variable..
Answer by sundhar_unity · Oct 27, 2020 at 04:49 AM
Check this Video For Random MoveMent : https://youtu.be/BIyYldTyyR8