Move an object by rotation
I'm trying to make a game where you move objects only on the X-axis by rotating them (a 2D-game, though I'm not using Unity2D right now, but I'm not counting it out either). So a ball is simple, the Unity roll-a-ball tutorial was exactly what I needed for that, however it's with other objects that I'm having problems.
A basic cube rolls without that many problems, however I've also got a cross-shaped object and a triangle-shaped object that I'd like to be able to move. With the cross shaped object, the longer I make it move the more it starts slowing down and "fighting back", when I release the key it jumps backwards and then settles down. The triangle is even less responsive, as it has serious problems even getting moving. Also, even though I've set them to freeze their Z-axis rotation and movement when I move them, they still move ever so slightly on the Z-axis, eventually falling off the level.
Here's the code I'm using for moving the cross, the other objects follow the same principle (except the ball, that's almost entirely the same as the roll-a-ball).
using UnityEngine;
using System.Collections;
public class MoveCross : MonoBehaviour
{
public float degrees;
private Rigidbody rotateObject;
public float speedRot = 10.0f;
public Transform moveObject;
public float speedMove = 6.0f;
// Use this for initialization
void Start()
{
rotateObject = GetComponent<Rigidbody>();
moveObject = GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
Vector3 transform = Vector3.zero;
transform.x = Input.GetAxis("Horizontal");
transform.z = 0;
transform = transform * speedMove * Time.deltaTime;
moveObject.position += transform;
if (Input.GetKey("left"))
{
rotateObject.transform.Rotate(Vector3.up, degrees * speedRot * Time.deltaTime);
rotateObject.constraints = RigidbodyConstraints.FreezePositionZ;
rotateObject.constraints = RigidbodyConstraints.FreezeRotationZ;
//break;
}
if (Input.GetKeyUp("left"))
{
rotateObject.constraints = RigidbodyConstraints.None;
}
if (Input.GetKey("right"))
{
rotateObject.transform.Rotate(Vector3.up, degrees * (-1) * speedRot * Time.deltaTime);
rotateObject.constraints = RigidbodyConstraints.FreezePositionZ;
rotateObject.constraints = RigidbodyConstraints.FreezeRotationZ;
//break;
}
if (Input.GetKeyUp("right"))
{
rotateObject.constraints = RigidbodyConstraints.None;
}
}
}
So I need to stop the object from moving any way on the Z-axis so that they won't drop off the level or fall over and I also need to find out what is hindering the movement of the cross and triangle objects. For the Cross, I tried both with a Mesh collider and with two Box colliders that I edited to fit the cross, the triangle had a Mesh collider. Thanks in advance!
Quick edit, seems like with the cross increasing the rotation angle at least helps with the object "fighting back", however the Z-axis is still a bother.