- Home /
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!
Answer by kevinspawner · Aug 19, 2014 at 10:27 AM
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
Add Rigidbody to the cube which will have the physics property.
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)))
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); }
Answer by bubzy · Aug 19, 2014 at 11:32 AM
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 :)
Your answer
Follow this Question
Related Questions
Trouble with moving cube when key is pressed 1 Answer
Cube rotates to the left on its own when moving 1 Answer
Erratics movements from my rigidbody : why does it behave like that? 0 Answers
Rigidbody slows down before touching its target 1 Answer
2D Game, Character Slides off Slopes, Need to be Able to Walk on Them 1 Answer