- Home /
How to Roll a Rigidbody Cube one step?
Hi. I'm trying rolling a cube with Horizontal inputs. i used "AddTorqe" , but after a while its flying in the air. i don't want that. i want to roll a cube like its heavy (not too heavy) and step by step. and when its in the middle of rolling to the right , its turn back when i press left key. and when i release the keys in middle of rolling, physics push it to the correct direction. i'm using this code. but no luck:
using UnityEngine;
using System.Collections;
public class rotate : MonoBehaviour {
public float torque = 100;
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float turn = Input.GetAxis("Horizontal");
rb.AddTorque(transform.up * torque * turn);
}
}
maybe code is good, but not the other setting like (torque , gravity, Mass , Drag , Angular Drag...). i don't know.
Any help will be appreciated.
Answer by JatsArts · Jun 12, 2016 at 01:20 PM
A quick and dirty solution is to add a physics material to the cube and increase the physics materials friction property.
Thanks . that helps. but still dosn't feel like its heavy. its rolling fast and when its do rolling a while, some times its get airborne. adding material friction is improve physics but increasing any of frictions dosn't affect anything.
Perhaps couple the friction with AddForceAtPosition()? Your image example hints at how it could be utilized -- If you apply the force higher up on the cube, it would effectively be like pushing near the top, rather than pivoting perfectly around the center of the cube.
Answer by thomasfriday · Jul 10, 2021 at 03:30 PM
Here's a short Youtube video that covers exactly how to roll a cube on its edges: https://youtu.be/06rs3U2bpy8