- Home /
How do I make a cube roll on a set forward path without it deviating
I'm trying to make a cube roll forward without having it bump left and right by it's own spinning. So physics causes it to spin around as it rolls, but it doesn't deviate from the path of going straight forward, and then have it be able to go forward in different directions without path deviation, but still allowing for physics to make the cube spin and jump off the ground due to spinning
Are your different directions always along an axis? $$anonymous$$g. along +ve x and -ve x?
No, it'll be sorta like rolling a ball around, my only issue is that a cube has edges so those edges continually cause some annoying deviations
Answer by TheBasementNerd · Nov 20, 2014 at 11:19 PM
I figured out a method, I created a 'move point' with the cube that sits in front of it however many units away. This move point is stored in a variable in the cube's script, and I have it combined with another section of code that allows me to turn it so that it always stays a set distance away from the cube, then I take the forward input (whatever it is set to) and apply force from the cube's position to the move point, multiplied by whatever so it goes faster:
var movePoint = Vector3.forward;
var dir = movePoint-transform.position;
function Update() {
if ((Input.GetAxis("Forward"+playerNum) != 0)) {
rigidbody.AddForce(-((movePoint-transform.position)*500)*Input.GetAxis("Forward"+playerNum));
}
if (Input.GetAxis("Sideways"+playerNum)) {
dir = Quaternion.Euler(0,3*Input.GetAxis("Sideways"+playerNum),0) * dir;
}
dir.y = 1.48
movePoint.x = dir.x + transform.position.x;
movePoint.z = dir.z + transform.position.z;
}
In my game the cube is scaled to 3, so 1.48 ends up being the center of the cube's height, so I keep the y on dir set to that so it can still bounce around naturally but doesn't travel up and down with the cube. Forward line motion isn't perfect, but it works great to steer a cube around
Answer by screenname_taken · Nov 19, 2014 at 10:33 AM
You could set up some layers for the physics so that the cube is colliding only with your floor. Then have a waypoint system (just some empty triggers) that the cube is set to look at. It'll rotate itself towards them and you'll just keep pushing it forward.
Answer by iz7749 · Nov 19, 2014 at 11:28 AM
You could use constant force in the direction you want and freeze the position under the constraints of the cubes rigidbody for the direction you don't want the cube to roll, play around with adding some relative force or torque in various directions and amounts to make the cube spin and bounce but because of the position constraint it will only roll in that direction. http://puu.sh/cWZMn/bf4ab38255.png Then i guess you could change that in code to alter what direction the cube rolls just remember to change the position constraint. probably not the best way to do it but should work for what you want sd you should be able to set up some triggers that when the cube hits it, changes the direction the cube rolls.
Answer by thomasfriday · Jul 10, 2021 at 03:32 PM
Here's a short Youtube video that covers exactly how to roll a cube on its edges: https://youtu.be/06rs3U2bpy8
Your answer
Follow this Question
Related Questions
2d Movement Messes up when cube is rotatated 0 Answers
Rotation along edge of a cube around another cube as its planet 1 Answer
Make an object rotate instantly upon an event occuring in C# 1 Answer
Tumbling a cube 1 Answer
Walking on a cube 2 Answers