Search Unity

Rolling a Cube

Discussion in 'Scripting' started by Muzza123, Jun 25, 2011.

  1. Muzza123

    Muzza123

    Joined:
    Jun 25, 2011
    Posts:
    4
    I'm new to the unity engine (I'm sure I'm not the first to say that)

    I want to make a cube move around by rotating on its edges, so when forward is pressed the cube rotates on the forward edge 90 degrees.

    I've had a search around for a tutorial or help but could find anything that worked, could one explain to me how to achieve this?

    Thanks in advance
     
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Is your cube just there and you have a player move and push it? If thats the case i think you can use transform.Rotate on the box when the player gets near the cube either using trigger(which i think is easier) or raycast. But if thats also the case, we need to check the axis that the cube is in. Since you say it have to roll forward, you have to make sure transform.Rotate rotates the cube according to it's local x,y,z, facing.
     
  3. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    Rolling a cube is challenging because as it rotates it has to move slightly up and down (otherwise the edge would go through the ground and that would look strange), so I think the easiest thing would to just animate it rolling and just play the animation while you move it forward.
     
  4. Muzza123

    Muzza123

    Joined:
    Jun 25, 2011
    Posts:
    4
    I found a tutorial that used transform.Rotate however when it does rotate the cubes falls through the ground.

    If I create an animation can I assign it to a key? So pressing Up activates the forward animation movement?
     
  5. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    What if you had an invisible object push the cube a certain way, rolling it?
     
  6. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    992
    1) use physics to roll the cube
    2) give the collider a high friction, low bounce physics matieral
    3) give the cube's rigid body a high drag to stop it flying away
    4) use add force to push the cube around
     
    BitGamey likes this.
  7. Muzza123

    Muzza123

    Joined:
    Jun 25, 2011
    Posts:
    4
    Could people go into more detail please, I fairly new to unity so I'm still learning what certain things do, Thanks

    Also I followed this tutorial http://unitynoobs.blogspot.com/2011/02/roll-cube.html
    I moves the cube the way I want it to but there is no gravity physics and the cube falls through the floor if the platform is raised.

    I would like to create the movement from scratch so that I get a better understanding of Unity and Java
     
  8. Muzza123

    Muzza123

    Joined:
    Jun 25, 2011
    Posts:
    4
    Just to go into more detail; when the player presses the right direction the cube moves right by rotating on the right sided edge (I hope this makes sense) I want to do this for all directions (up, down, left, right).

    With this I also want gravity so if the cube moves off the platform it falls down, I can't seem to get both the movement and gravity at the moment.
     
  9. Tarodev

    Tarodev

    Joined:
    Jul 30, 2015
    Posts:
    190
    Here's a quick tutorial on how to achieve it:
     
    Nad_B likes this.
  10. chfr8d9

    chfr8d9

    Joined:
    Feb 29, 2024
    Posts:
    2

    cuz my cube has a mass of 1e+09 lmao