Is there any flying bug scripts out there?

Hey everyone! I’ve had Unity for about a month now, and I figure it’s time to make my first game. It’s going to be more of a simulation game than a regular game, but it’s going to basically be about this robotic fly in a modern-day living room. However, I’m still just learning Javascript, and I don’t have the ability to create a code as of right now that allows the fly to “fly,” move forward, backwards, left and right.

So far I’ve got this, but it’s very choppy/not smooth as I would like it. The game is a simulation game and I would love it to have a nice, smooth feel to it.
#pragma strict

var walkspeed: float = 5.0;

function Start() {



}

function Update() {

    rigidbody.freezeRotation = true;

    if (Input.GetKey("space")) rigidbody.AddForce(0, 2, 0);
    if (Input.GetKey("d")) transform.Translate(Vector3(-1, 0, 0) * Time.deltaTime * walkspeed);
    if (Input.GetKey("a")) transform.Translate(Vector3(1, 0, 0) * Time.deltaTime * walkspeed);
	if (Input.GetKey("w")) transform.Translate(Vector3(0, 0, -1) * Time.deltaTime * walkspeed);
	if (Input.GetKey("s")) transform.Translate(Vector3(0, 0, 1) * Time.deltaTime* walkspeed);
	
}

The flying is triggered by pressing the space bar and the rest is just wsad movement, I just found this script somewhere on the forums and edited a little bit. So back to the question, is there anyone that has a better script out there for flying bug movement? Or, can you please edit this one to improve it?

Any help is greatly appreciated, and thank you in advance!

AddForce works more smoothly in FixedUpdate, and you don’t have to freeze rotations every Update. You can also simplify a lot the code with Input.GetAxis:

var walkspeed: float = 5.0;
 
function Start() {
    rigidbody.freezeRotation = true;
}
 
function FixedUpdate(){
    if (Input.GetKey("space")) rigidbody.AddForce(0, 2, 0);
}

function Update() {
    var dirX = Input.GetAxis("Horizontal"); // read A/D keys
    var dirY = Input.GetAxis("Vertical"); // read W/S keys
    transform.Translate(Vector3(dirX, 0, dirY) * walkspeed * Time.deltaTime);
}