Monster shadow


Rolling a cube - Unity Tutorial

Roll a cube. You'd think it'd be straightforward... But there are a few tricks to it. Let me show you my guy.

Эрбол Бекзат уулу
Great tutorial btw! I noticed that if you (90 / _rollSpeed) is not a whole number then you RotateAround past 90 degrees. To remedy this I set the angle to be based off the remainder of the last angle. This way you can set the _rollSpeed to whatever value you wanted e.g. 3.84 private IEnumerator Roll(Vector3 anchor, Vector3 axis) { _isMoving = true; var rotationIteration= 90 / _rollSpeed; var rollAngle = _rollSpeed; float currentRotationAngle = 0; float previousRotationAngle = 0; for (var i = 0; i < rotationIteration; i++) { currentRotationAngle += rollAngle; if (currentRotationAngle > 90) { rollAngle = 90 - previousRotationAngle; } transform.RotateAround(anchor, axis, rollAngle); previousRotationAngle = currentRotationAngle; yield return null; } _isMoving = false; } Curious to here your thoughts on if there's a better solution that you can think of.
Interesting things start to happen when you press 2 keys at the same time e.g. "W and A" or "A and D" Seems to be remedied by ensuring only one of the keys is pressed using "else if": if (_isMoving) { return; } if (Input.GetKey(KeyCode.A)) { Assemble(Vector3.left); } else if (Input.GetKey(KeyCode.D)) { Assemble(Vector3.right); } else if (Input.GetKey(KeyCode.W)) { Assemble(Vector3.forward); } else if (Input.GetKey(KeyCode.S)) { Assemble(Vector3.back); }
How would you setup collisions?
You'd need to manually check for collisions before rolling and prevent the roll all together. Simple raycast 1 unit across would work.
MoMak Playz
Hey i just discovered you through an ad, love ur channel of teaching unity! Please post unity tutorials in an easy way to understand and il always watch ur videos! LOVE THE CHANNEL!!!!
Kriss Loo
Totally more effecient than whatever I did xD Loved it <3
Kriss Loo
Found it :)
Itching 2 Play
Got here from an ad, but solid video. Take my like lol
Ammon Law
Same here!
Yessss! My ad works 😜
Bruno Medeiros
Any tips on how to roll the cube above another cube? Should I be changing the anchor or any other type of logic?
I have solved the problem with climbing (rolling cube above another cube), I can help you if needed after 3 months, but there is another problem - rolling down off a cube, you can't really detect with raycasts if there are blocks under next move
@Bruno Medeiros I believe you also need to rotate 180 degrees to get on top of the adjacent cube from its side.
Bruno Medeiros
@Tarodev Thank you so much! :)
You need to choose a new anchor point when rolling. So it'd be the top of the cube instead of the bottom. Something like this: var anchor = transform.position + (Vector3.up + dir) * 0.5f;
Very good tutorial! But I am new in game developing and walked into a problem as when I hold A and D it glitch out as I am trying to walk both directions or when I hold D and W it will go perpendicular direction. I though problem was that its trying to do a task while _isMoving but I didn't find a solution. Do you know what could be the problem?
@Tarodev oh yeah that makes sense now! Thank you!
Make sure you're using else if instead of 4 if statements. That will ensure only one direction will trigger at a time
Just saw your ad
I totally forgot I had that running... Glad you made it :D
sometimes i bless the youtube ads god👏
Showed you what you needed to see? 😂
Sören Blocksdorf
Oh s**t. Here we go again xD. Still great video and thanks for the effort
Do I have a deja-vu, or was this posted a few days ago? xP
Chahd Studio
@Matthew Spencer any news about the tutorial? thank you Matthew
Chahd Studio
@Matthew Spencer can't wait, thank you, good job
Matthew Spencer
@Chahd Studio I'll be doing a tutorial on my scenes in the future :) Stay tuned.
Chahd Studio
@Tarodev me too i was going crazy!! by the way can you share your scene set up (lighting/render setting/post processing/shaders)? very cool look
You're not going crazy! I had an error in the code so I refactored and made it better 😅


in this video i'll show you how to achieve this satisfying cube rolling
effect in just a few steps okay let's start by creating a c-sharp
script or player controller whatever you'd like
all right so let's start by capturing some player input uh
get key again and let's make it left for now so
when rolling a cube you can't just rotate it from the center
because if you rotate it from the center you'll obviously get
this kind of effect so what we need to do is we need to find a
anchor point which will be when turning left it will be down here
and then we need to rotate it over that pivot point there
okay so to get the uh anchor point let's say anchor equals
transform.position plus new vector 3 and
we're going to be moving half a unit left
and then half a unit down now we're so uh
so left 0.5 and then down 0.5 and then on the z we're doing nothing so
there's our anchor and then to find our axis so we need to
know which uh which axis the cube is going to
actually rotate on and for left it's as you can see it's on the z
axis there so to find that out we can do vector three cross and we'll take in
uh yeah we'll take another up vector three
up and then uh as this is left we'll say vector three
left three left and then that will give us our cross
so what vector three cross does is it finds the perpendicular
uh axis when given two other axes so for example we're giving it uh up
and left so y and then uh left on the x so the perpendicular would actually be
uh the z axis which is uh perfect for us because
we're wanting to rotate on the z axis okay so now that we've got our
anchor and our axis let's create the actual roll script so i
numerator let's call this roll and this will take
a vector3 anchor and a vector3 axis
and then we want to say we only want to accept movement
while we're stationary so let's create a private
bull is moving and then here let's set it is in fact
true and then we'll do our movement script here
and then let's just say uh is moving down here sent to false
so while we're moving let's so basically we need to rotate 90
degrees uh and we want to do that over time so
let's create here a private actually let's we need to
serialize this so private flight and then roll speed let's depot
this to three so then here we're saying we need to
roll 90 degrees uh and we want to split this up uh
over our roll speed so 90 divided by roll speed
okay and then in here we will be saying transform dot rotate around
whoops it's not transform transform dot rotates
around and we will be sending in our anchor
because that's the uh this is not the correct one so this is the
correct one here so the point that we're rotating around
the axis which is in our case the z and then the angle which will be our
roll speed so access there
and then roll spade just like that and then we will yield return here
just so it doesn't all happen at once uh wait for seconds
let's do 0.01 like that and that should already do the job so
then here we can stay start co-routine roll and we'll send in
our anchor and uh access like that
and up here let's just prevent anything from happening if we're already
moving so let's just return straight out of our update function if we're moving
and let's check to see how that works so put our player controller on the cube
roll speed set three let's try that and go left
there we go perfect so the only thing left to do
is to do this code for all the directions
but we obviously don't want to repeat this code for everything so let's create
a little um a little function here a little
internal function let's call it assemble
and then this will take in a vector3 direction let's copy this code and put
it in here and now we don't want to hard code this
anchor point so we can infer that from the directions so
let's do this vector 3 down
plus direction [Music]
times 0.5 as we're only going half a unit
and we will replace the left here with our direction
and then we can just use this here and we can say
vector3 left and then let's copy this a few times
and we'll say this one will be right and then this one will be forward
and this one will be back okay let's check that out press play
and there we go we've got a rolling ball now we obviously don't want to be
pressing pressing the button every time we want to roll so let's do a little bit
of changing here we can change that from down to just get key
now remember this won't still happen every frame because as soon as we
assemble here we are starting moving so this is just
going to return until we're finished so let's just clean this up a little bit
and there we go that should just be about it
so if we up our move speed a little bit something's like nine
and there we go a rolling cube pretty easy not too much code
easy to understand so if you enjoyed it uh
do what that text says there and i will see you in the next
video see ya foreign