Monster shadow

VIDEO

Rolling a cube - Unity Tutorial

Description:
Roll a cube. You'd think it'd be straightforward... But there are a few tricks to it. Let me show you my guy. Script: https://pastebin.com/z9JpJtW3 Vector3.
ADD A COMMENT

Jordan Williams
Got here from an ad, but solid video. Take my like lol
Tarodev
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?
Bruno Medeiros
@Tarodev Thank you so much! :)
Tarodev
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;
omgits_cooper
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?
omgits_cooper
@Tarodev oh yeah that makes sense now! Thank you!
Tarodev
Make sure you're using else if instead of 4 if statements. That will ensure only one direction will trigger at a time
MIKK SDH
Just saw your ad
Tarodev
I totally forgot I had that running... Glad you made it :D
Newt
sometimes i bless the youtube ads god👏
Tarodev
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
Thedragonixxx
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
Tarodev
You're not going crazy! I had an error in the code so I refactored and made it better 😅
SEE ALL COMMENTS


Transcript:

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