Code not working properly

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.

float speed = 3;

Vector3 MoveDir(){
    return Vector3 moveVect = new Vector3(random1(), 0, random2());
}
Vector3 MoveVel(){
    return MoveDir().normalized * 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;
}
1 Like

sry I was tired then and not thinking straight. no the voids in my functions should not be there, and i have changed my original post accordingly

As for why it doesn’t work, you will have to elaborate more on what you mean by “doesn’t work”. is it

  • not behaving how you expect, or
  • throwing errors

if it is throwing errors, what are the errors.

Thank you, this works