Check out a free preview of the full Game Development with Unity for Web Developers course

The "Falling Sphere Solution" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:

Nick walks through the solution to the sphere exercise.

Preview
Close

Transcript from the "Falling Sphere Solution" Lesson

[00:00:00]
>> Well, hopefully again, you didn't find this one too difficult. It's a little more challenging than previously. And we have one more that is slightly more challenging, but here's the solution that will then code up. So it's pretty simple. Just like the comment says, if the y positions, how do we get the y position?

[00:00:25]
We got the transform.position.y of the game object is less than or equal to the game manager respond height. So that's the property that we created in game manager that stores that value o 0.25. If that's true then we call reset position that's in the rolling movement component. So let's jump back into Unity and do that.

[00:00:56]
So first, I just wanna show you what happens without that. And it's probably what you'd expect. So hit space to get started. And if I roll off the side, it's just going to keep falling into infinity and hit terminal velocity of gravity. And you can actually see in the transform, there's the y value just increasing to negative infinity, we don't want that we want it to actually reset.

[00:01:27]
So let's open rolling movement and in fixed update just below add force will say if the y position of the sphere is below the respawn height. Reset its position, okay? And we want to say if transform.position.y is less than or equal to the game manager respond height, then reset the position, there's a reset position and that will reset the velocity, the angular velocity and the position.

[00:02:22]
So, we'll save that switch back over to Unity. And now if I hit Play, And we hit the Spacebar to start the game, and I roll off the side, it should keep responding me in the same spot and it will drop me back in. And that will also be true for the opponent.

[00:02:50]
I've never actually tried to do this before, but maybe I can knock the opponent off the side here. Nope, I'm so close, okay, there we go. The opponent's dropped back into yeah

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now