Hello I am new to unity And trying to move an cube Object in x direction so I wrote a simple code.
private Rigidbody rb;
public float forwardSpeed;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
///moving in X direction
Vector3 moveForward = rb.velocity;
moveForward = new Vector3(forwardSpeed, rb.velocity.y, rb.velocity.z);
rb.velocity = moveForward;
}
when i play it the cube doesn’t move ,but in inspector panel its X and Z transform component keeps changing even though the forwardSpeed is set to 0 . The component values keep changing back and forth from -1 to -8 and when i set forwardSpeed to 5 the cube rotates and moves in x direction and falls down.plzz Help I cant figure it out why that’s happening.