Global vs Local Space
Try reloading this page, or reviewing your browser settings
In this video segment, learn how to rotate an object to face a target object in games, such as in tower defense games.
- Facing objects
- Target objects
About this video
- Alan Thorn
- First online
- 12 January 2019
- Online ISBN
- Copyright information
- © Alan Thorn 2019
[Audio Begins] [0:00:00]
Alan Thorn: Fairly often when creating games, in particular games like tera defense games, you’ll want to create one object that rotates to face a different object. It doesn’t really matter where the destination or the target object moves, you’ll always want your source object to face it. In this movie we’re going to focus on how to create that kind of behavior. Specifically, we’re going to create an object that can look at a target object. In the case here, in this scene that we have, we have the car seen parked on the driveway. I’m going to create a kind of sample object that might be a gun tote or a security camera. And this object is going to continually turn to face the car is it moves throughout the scene. I’m going to be picking up this project from the preceding movie where we had player controls on the car, so that I can use the input keys on the keyboard to control the movement of the car moving around the scene.
I’m going to create my sample gun tote object by using a two-stage system. Firstly, I’m going to create the base of the target by creating a cube. I’m going to position the cube within the scene. So slightly over to this side here. I’m going to move that over to here, and I’m going to flatten that down just a little bit. I’ll move it down to the bottom, actually stretch that up a little bit to about there. And then I’m going to bring in a second object. I’m going to right-click, choose Create, and then choose Cube again. For the second cube, I’m going to stretch that out and to narrow that down just a little bit here and bring that on top, so that effectively we’re going to have that object continually turn to face the car here.
Now the next thing I want to make sure of, is that we select the correct pivot point. Right now, the pivot point for this object is at the center here. So in addition I’m going to create a new empty object, which I’m going to center here at the top of this cube. So about there. I’m going to rotate it so that the forward vector here is facing the same direction as this cube here. So I’m going to rotate that here about negative 90, so that the forward vector is pointing in the same direction as this cube. I’m going to call this object the pivot, like so. And I’m going to call the base here, the tote. And I’ll call this cube the site. This is going to be a child of the pivot, so that as I grab the pivot and rotate it, then it will turn to face the object needed.
Now, I’m going to create a script file that I’m going to attach to the pivot, which will control its rotation to look at this destination object. I’m going to go into the scripts folder. Right-click and choose Create, and then select C# script from the menu. And I’ll simply call this script Look At Target, like so. Again, notice the generic name so that this could be attached to potentially many different objects to create the behavior of looking at various targets. I’ll drag and drop the Look At Target script onto the pivot point, making sure that it’s attached here in the object inspector. I’ll double-click the script file to open that inside visual studio here, and there isn’t very much that I need to do.
First of all, I’m going to create a private variable to the transform component as I have done to many of the other script files before. Inside the awake function, I’ll retrieve a reference to the transform component simply by using the Get Component function. We’ve seen how to do that many times. So I’m simply going to put transform between the brackets here. Inside the update function, here we have one of the most important parts which is continually ensuring that the transform component rotates to face a direction. Now of course, we need to tell it the direction to face. And so I’m going to need a completely new variable here which is going to be a reference to the transform component that I want to look at. I’m going to call it target, and effectively, it’s going to be the player character. Inside the update function, I’m going to choose transform, and there is a really convenient function called look at. It requires really only two parameters or in this case it actually requires one parameter, but there is a two-parameter version, which simply requires you to specify the points, Look At, and which way is up. But in this case here, it is simply telling me look at the target. And I can specify which target I want to look at. In this case, it’s going to be my target field here. So I’m simply going to say target and press Ctrl-S to save that code. I’m going to minimize that and go back to Unity here and select my pivot point. When the code compiles, I’ll get the target field that I can specify in the inspector, and I’m just going to drag and drop that here. So in this case it’s the car root. I’m going to drag and drop that into target field.
Now that it’s been specified, this is going to constantly rotate to turn and face the relevant target. We’ll see that taking affect here inside the game tab. So you can see this is now looking. The forward vector is pointing towards the car. And as the car moves through the scene, the target is continually changing to reflect that. It really doesn’t matter where I move the car, it’s always going to turn to face it. So I could turn the car to move around the scene. And you’ll notice slowly, the target continually faces the car. So that’s pretty useful. That is how we can make one object turn to face another.
In this movie, we saw how we could use the transform.lookat function to make a source object like a gun tote, continually turn to face a destination target. This is going to be really useful in a different variety games for creating gun totes, security cameras, and even the heads of characters that need to turn to face their counterparts.
[Audio Ends] [0:06:17]