- Home /
How to Properly Translate Across Single Axis?
What I'm attempting to do is get an object to move across a single axis (think 2D game). To get the framework up and running, I've tossed out a large "Block" prefab and various other prefabs on top of it (to see how smooth each moves). I've tried this with Blocks, Capsules, Capsules with Block Colliders, and Spheres. They all sit on the "ground" properly. All have Rigidbody Controllers as well (for the gravity).
Now, for the movement, I just want the objects to go along the X-Axis. To do this, I've attached the following C# script for movement:
if (Input.GetKey ("right")) transform.Translate(Time.deltaTime,0,0);
This is located in the Update function and it works -- each item is pushed along the X axis. The problem, however, is that as an item moves, even along a flat plane, it "bounces" a bit (like it's on a rocky surface), making it twist and turn along the Z axis as well, eventually falling off the plane (although it should ONLY be moving along X and not Y/Z).
I've tried using Freeze Position/Rotation on Z and it still has no effect, as each thing still twists and turns (even the sphere).
Also, the plane is set with Rotation of 0,0,0, as are each of the other items, so they should all be flat.
I've tried using various methods of scripting to try and get the objects to slide around the X axis but they always end up with the Z-style turns and falling off. Any help resolving this would be much appreciated.
Thanks!
You want to use a character controller ins$$anonymous$$d of a Rigidbody.
@Robertbu Thanks for the input! That worked for making it smoothly translate, but now there's no gravity. Is there any way to enable gravity with the character controller? There are gaps between platforms and such I want the objects to be able to fall through.