top of page

Me and My OC Part 4 - Character Movement 2

  • Writer: Joses Kang
    Joses Kang
  • May 23, 2021
  • 3 min read

Hi guys, Joses here. Today, we’ll be concluding our tutorial on making your own OC asset. For our last part, we’re continuing with character movement for our robot!



So, how do we get the ball rotating in the direction of motion without rotating it with the head? Well, it's quite simple really. We duplicate the sphere! As seen in this video, we'll duplicate the sphere and remove all of the components except for the Sphere Collider (which we remove from the original Sphere Collider!)


For clarity sake, I'll be calling the original sphere the Visual Sphere and the duplicate the Collider Sphere. In order to achieve the type of animation that we desire, we can force the Collider Sphere to always face the direction of motion along the horizontal plane that we intend for the character via WASD. This will allow us to always have a local x-axis to rotate the Visual Sphere around.



Over here you can see the variables that I called before the Start function. If you recall from the previous chapter of this tutorial, we used public to expose variables to the Inspector. While that is convenient and efficient, it also has the added effect of exposing those variables to other scripts! To avoid that, we can put the line [SerializeField] with a private variable and it'll still be exposed to the Inspector! Pretty cool, right?


For easy reference, drag the Visual Sphere into sphereObject, the Collider Sphere into sphereCollider and set the sphereRotationSpeed to a manageable amount.



Now, let's write the code for the rotation to happen. First, we'll create a function called RotateSphere. As mentioned earlier, we need to force the Collider Sphere to always face the direction of motion along the horizontal plane. The first two statements handle that.


Next up, rotating the Visual Sphere. We get the axis and amount to rotate by via:

sphereCollider.transform.right * distSinceLastFrame * sphereRotationSpeed

After that, we rotate the Visual Sphere with the Rotate method and store the current frame's coords for the next frame.



As you can see here, we run RotateSphere for every time that there's an input from the player. This allows the Visual Sphere to only spin when we're intentionally moving. If you want the Visual Sphere to rotate when the model is being moved without player input, you can just call RotateSphere once outside of the if statements within the Update function.



This is what our little buddy looks like now!



Now, let's talk jumping. The concept for all basic jumping functions in games are usually the same. If the player is touching the ground, we let them jump. If not, we don't. Simple enough, right? This clip shows me placing a trigger around the bottom of the sphere. This trigger will let us know if the ground has entered it, hence telling us that the player has touched the ground and can jump!



In this clip, we're giving the floor a "Floor" tag. This allows us to identify and flag different objects as the floor by simply giving them this tag.



These are the variables that I set before Start.



These are the lines of code that handle the jumping. Like I mentioned earlier, we'll allow the player to jump if they're touching the ground. We can check for that via the OnTriggerEnter and OnTriggerExit methods.


We're going to want to let our character jump regardless of WASD input, so we'll create a new if statement to check for Spacebar input.



One more thing, we're going to set constraints on the Rigidbody of our character. This refrains the character from falling over. We'll only freeze x and z rotation because we want the character to still rotate along the y-axis.



That's about it! This is what my little guy looks like roaming and jumping around.


We know you guys love freebies, so here's the project file:


We hope that you guys enjoyed this tutorial! Stay tuned on our blog for more great content! This is Joses signing out. :))

Comments


logo_text.png

    ©2021  Golden Monkey Studios

    bottom of page