I want an object to move randomly and I’ve tried this code:
using UnityEngine;
using System.Collections;
public class FloorRandomMovement : MonoBehaviour {
public float randomNumber1;
public float randomNumber2;
public float moveSpeed;
public Vector3 randomMovement;
public Rigidbody rb;
// Update is called once per frame
void Update ()
{
randomNumber1 = getRandomNumber();
randomNumber2 = getRandomNumberTwo();
//print("Number one is: " + randomNumber1 + " and Number two is: " + randomNumber2 + ".");
randomMovement = new Vector3(randomNumber1, 0f, randomNumber2);
print("Random movement is: " + randomMovement);
rb.AddForce(randomMovement * moveSpeed);
}
//gets first random number
float getRandomNumber()
{
float number = Random.value;
if (number >= 0.5f)
{
number = number * (-1);
}
else
{
number = number * (1);
}
return number;
}
//gets second random number
float getRandomNumberTwo()
{
float number = Random.value;
if (number >= 0.5f)
{
number = number * (-1);
}
else
{
number = number * (1);
}
return number;
}
}
The object only moves in one direction and that doesn’t seem to make sense as the way I see it, it should move randomly. Any suggestion to improve code or is there a better way to make an object move randomly?
The way your script works, it adds force to the object at every frame. so if it is moving in a single direction, it means RNGesus deemed the sum of random forces exerted is greatest in that direction.
Remember, you are adding forces, which means you are only mildly influencing the object, nudging it slightly every frame.
if you want the object to change its movement direction completely, while still sticking to using Rigidbody, you will have to manipulate the velocity directly, not add force.
To manipulate velocity, you will have to set a desired speed you want your object to move at represented as a float value, normalize your movement vector when you generate it, and multiply your normalized movement vector by the desired movement speed.
instead of adding force, you define the speed of the Rigidbody directly.
rb.velocity = MoveVel();
Assuming you make these changes and are now changing your object’s velocity directly, you may find changing movement direction on every frame extremely erratic, so you will want to setup some logic for periodically changing direction instead.
As another observation, there is no need to define two functions to get your random direction. if you are generating a new random number on every call of the function, the function give a different value on every call.
This doesn’t seem to work. Is there a reason for that?And when you are trying to make the function, what am I trying to return, having both void and Vector3 there makes no sense.
Those should be Vector3. And also, as mentioned, you’d definitely want to change velocity every X seconds instead of every frame. This should be in a coroutine or an InvokeRepeating, not in Update.
In the original method, the reason it tends more in one direction than the other is because you’re generating asymmetrical random numbers. You’re making the high values negative and the low ones positive, which will move it in the -X,-Z direction more often than not. For a random XZ direction, you want something like:
Vector3 GetRandomDirection () {
return (new Vector3(Random.Range(-1f,1f), 0f, Random.Range(-1f,1f))).normalized;
}