- Home /
How to move/rotate cube forward/back/left/right in C#?
Hi everyone!
I'm trying to make a cube move and rotate (forward, back, right and left) using a C# script.
Still, I have a slight problem which is that I can't change the axis on which I move my cube any time I want. I can move/rotate it forward and back, no problem. I can move/rotate it right and left, no problem. But if I move it 2 times to the left and then forward for example, the cube doesn't move the way it should anymore.
Are you hoping for a realistic physics-like topple? Or does the topple have to be an exact precise movement every time?
Hey Blackup and thank you for your comment. I'm looking for an exact precise movement every time since the cube is the player controller on a virtual board game (well, that's my goal anyway).
Answer by flashframe · Apr 09, 2015 at 02:01 PM
You just do this
http://answers.unity3d.com/questions/710087/tipping-a-cube-over.html#answer-710471
Note that if yOU DO want to do it with physics (rather than "abstractly" as in a puzzle game), then: Add a Rigidbody
to your cube and add torque. (You'll need a floor under your cube)
public float torque;
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float turn = Input.GetAxis("Horizontal");
rb.AddTorque(transform.forward * torque * turn);
}
Tweak the torque variable until you get enough force to topple the cube.
Thank you flashframe. It's way easier indeed but I'm afraid I'm looking for a precise movement as if my cube were a player controller on a board game and you need to topple the cube exactly from tile to tile.
$$anonymous$$aybe there's a way to make your solution precise?
Fair enough. How about using the script in this answer? It uses a parent object to rotate the cube, and finds the edge that is on the floor before rotating. Looks promising.
http://answers.unity3d.com/questions/710087/tipping-a-cube-over.html#answer-710471
Thank you so much! It works almost perfectly, I'm just gonna need to tweak it a little bit but that's great! Thanks again!
Answer by Blackup · Apr 09, 2015 at 06:17 PM
Remember that with "transform.Up/forward/back/left/right/down" these are all relative to the object's orientation. The reason why your topples fail the second time... is because after the first topple, transform.up isn't up anymore. The object might now be lying on it's side, so transform.up might actually be pointing left...
So what does this mean? To make your rotations based on the world's notion of up, forward, right etc... you should think in WORLD space instead of local space... so use Vector3.up/down/right etc... instead. This will give you the world axis in those directions... they won't change and will always be the same no matter how many times your cube rotates.
I thought it was related to the local directions but I couldn't figure out how to solve the problem.
Flashframe gave me a JS script that works almost perfectly and which is less complicated than my C# script. I think I'm gonna use it but I'd like to correct my script anyway just for the heck of it and I believe your answer is gonna help me do that so thank you for your answer!
Best exercise for understanding the logic is to translate the java script to c#. Sometimes this is hard work, but the experience and understanding is golden with all future DIY scripts. I'm very impressed with your script though. It shows a lot of intuition and initiative.
It's true that the OP did not know about global coords, but the way this is done in vid games is just with a "rotational marker" parent. It's an everyday technique.
Answer by Fattie · Dec 12, 2015 at 07:17 PM
This is a well-known trivial technique in vid game development. It's a duplicate of 100s of identical questions on here.
full tutorial ... http://answers.unity3d.com/questions/1111304/roll-cuboid-doesnt-work-properly.html#answer-1111309
You just move a "rotation point" around and then flexibly "re-parent" your cube to that.
It's a basic everyday technique to master in vid games.