Understanding Movement and Rotation in C#


Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video segment, learn to code the movement of an object while considering others in its path.


  • Physics objects
  • RigidBodies
  • Obstacle consideration
  • Adjust code

About this video

Alan Thorn
First online
12 January 2019
Online ISBN
Copyright information
© Alan Thorn 2019

Video Transcript

[Audio Begins] [0:00:00]

Alan Thorn: In this movie, we going to explore some of the potential problems that arise when using the transformation work that we’ve seen so far in this course. In particular, we’ve seen how to move objects in particular directions at specified speeds, and that all works wonderfully. However, one of the central problems we still have is that the objects that we can move do not take into account the physical obstacles inside the scene, so they’ll actually pass through physical obstacles. In this movie, we’re going to see how to solve two major problems. One, how we can configure our objects so that when they do move, they take account of the physical obstacles. And two, how we can adapt our code so that our objects when they move, they will bump into things instead of passing through them.

Let’s go back to our scene that we’ve created here. The car scene here situated on the driveway. You’ll see that if I move the car, for example, let’s say that I move the car down here. And I’m going to rotate that here by about 90%. Make sure on the Y axis that I’ve rotated it by 90%, and then I’m simply going to press the Play button here on the toolbar. Now when I do this the car will move forwards inside the scene. Let’s move it forward just a little bit further, but notice as I’m doing it here, the car passes through the wall here, or through the fence, and it continues off the driveway. In fact, it doesn’t even fall to the ground under the effects of gravity here. So this is one of the major problems with this vehicle here is that in fact it doesn’t really take into account any kind of physical obstacles. And I want to show you how we can solve that.

I’m going to jump back over to our mover script here to bring that up inside visual studio. Now typically, we’ve been using the transform component to simply change the position of the object inside the scene. Now this works great, but the transform components doesn’t actually validate the positions that we specify. It simply allows us to change the position of objects, but it really doesn’t care if those positions are valid inside our game world. It will allow us to position two objects intersecting one another. So the car could be positioned inside and part way through a wall, and Unity is totally fine with that if we go about changing the position of objects using the transform component. But I’m going to adapt this code instead to use the rigid body component. That allows us to make our objects compliant with the physics system.

To do this I’m going to minimize visual studio here. I’m going to select the car object and change some of its properties. You’ll notice if I select the car object, this mesh object in fact has a mesh collider. I’m going to deactivate that mesh collider to remove that from the car object and select the car root. I’m going to choose component, and then choose physics, and choose box collider to create a basic box collider around the car object. You can set the car object or rather the box collider inside the car. It’s intersecting the car here. But I’m going to move to the box collider component and change some of the properties so that effectively the box collider just approximates the rough volume of the car to something kind of like that on the X axis. And then to scale that outwards on the Z, and also a little bit on the Y, so that we have a vehicle that uses this kind of collider here. With that in place I’m also going to choose component, physics, and then select rigid body, to add a rigid body component to the car here. I’m just going to raise the car up a little bit here and press Play on the toolbar. And when I do that you can see that the car is indeed moving around. In fact, it’s colliding with different objects here. Now one thing that I want to make sure right now, is I want to place some constraints on this, so that effectively the car, the rotation of the car cannot change as a result of physical collisions. It’s only the position that can change here.

I’m going to press Play once again here and start moving the car around the level. And see that the car is moving around. It’s not quite touching the ground, but that’s simply because I’ve added this collider object here. So you can see right now the car is kind of pushing through these physical obstacles, but I’m going to make sure that, in fact, I change the position of the car not by using the transform component, but by using the rigid body component attached to it.

I’m going to go back here to my mover script and change the transform here away from transform of rigid body. So I’m going to create a new private variable that I’m going to call rigid body here. And I’m simply going to call it ThisRB for this rigid body. Inside the awake function, I’m going to get access to that component. So I’m going to choose get component, rigid body. And any time that we want to work with physics or physical reactions inside the scene, we don’t want to use the update function. Instead, we want to use the fixed update function if we’re going to be working with the physics system. So I’m going to choose fixed update here.

I’m going to comment out our original line. And this time I’m going to replace it with a rigid body statement here. By choosing rigid body, we can control the velocity of an object. This is simply a vector that represents both our direction and our speed at which an object should travel. So I’m going to choose equals here, and I’m going to choose our forward vector. And then I’m simply going to multiply that by our speed, and then the statement here. We don’t need to use Time.deltaTime because the physics system already uses that. So I’m going to choose Command-S to save this, go back to the scene here, choose Play on the toolbar, and when I press Play, the car begins moving inside the scene. Not only will it continue moving, but it will also take into account the different obstacles inside the scene so that it simply won’t pass through them. For example, if I move the car over to this side here. In fact, let me just press Play and move the car kind of along here, bring it forwards slightly. Oops, actually, I’ve just adjusted the position of the car in relationship to the root object here. So I’m going to bring that to about here and press Play on the toolbar. So the car is moving forwards in the scene. It’s going to approach the fence at the level here. Notice this time it doesn’t pass through the fence on the level, and that’s because now it’s taking into account the rigid body component of the car and the physical obstacles of the scene.

In this movie, we saw how to code the movement of an object so as an object moves forward along their forward vector, they don’t simply pass through solid objects like they’re a ghost or immaterial substance. Instead, they take into account the physical obstacles on the level and will not pass through them. This is because the rigid body component is validating the new position that we specify.

[Audio Ends] [0:07:46]