Getting cube movement down

Hello people!

So I’ll start this off by saying I’m extremely new an Unity, and after spending a LOT of time googling and reading through these answers I have to say I’m still completely stumped with my problem.

So for the game I’m playing around with, I have a cube on a plane that I want to move. However, if I use rigidbody.velocity to move the cube around it rotates everywhere, and even if I freeze the Y position and all of the rotations it still moves wonky which is expected when things are based on velocity. (takes awhile to slow down etc)

So I tried transform.position, however if I use that then the collision detection provided to me with rigidbody is not placed properly, and things go crazy.

Basically, all I want is the cube to move on the X and Z axis with no rotation smoothly, and to collide properly with walls that I make.

Thanks for any help!

  1. Make sure you have collision on both game objects. IN your case plane and a cube. Box collider is cheap and faster compared to mesh collider. But for testing purposes you can use mesh collider it will have a proper collision

  2. Add Rigidbody to the cube which will have the physics property.

  3. In the script. You can use Rigidbody. Addforce to create a force and can move the object in x and z axis. Also for keyboard inputs you can use something like if((Input.GetKeyDown(KeyCode.W)))

  4. paste the code in the script and attach it to the cube.

    void Update ()
    {
    	float MoveForward = Input.GetAxis("Vertical") * 10 * Time.deltaTime;
    	float MoveRotate = Input.GetAxis("Horizontal") * 10 * Time.deltaTime;
    
    	transform.Translate(Vector3.forward * MoveForward);
    	transform.Rotate(Vector3.up * MoveRotate);
    
    }
    

you shouldn’t really alter the velocity of a rigidbody, instead use addForce()

public float speed = 1.0f;
    	
	// Update is called once per frame
	void Update () {
	
		if(Input.GetKey(KeyCode.UpArrow))
		{
			rigidbody.AddForce((Vector3.up)*speed);
		}
	}

of course, you can just alter the Vector3.up to match the direction that you want your object to travel in

hope this helps :slight_smile: