Home
Username:
Password:
Introduction to Game Development Using Unity 3D Tutorials

Interacting with Models / Raycasting




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

Let's recap what we have so far. When I play my game, you can see that I can shoot crates, we've got that covered and we can walk into the door and then the door opens. Now, next thing to bring our game out of Dorky Land is to make sure that when we're colliding with the door, it doesn't open when we're already hitting our nose on it, but that it opens before we arrive there, just a like a supermarket door would do. Now, to do this, we will use a technology called Raycasting. Now, Raycasting is pretty easy to understand. Look, for example, with this player and the move handle that is sticking out of him. A Raycast would be an invisible line, about this long as what we're going to make it and this line checks if there's collision at this certain length. So, that way, if this line, invisible line collides with the door, it will open it for us. Pretty easy stuff and it's not going to be very difficult. Alright, so, first of all, we need to get rid of this code but let's not delete it yet, because we might need it later or if something doesn't work, you wanna go back to it. So, to uncomment it, just type/asterisk at the front and then an asterisk another/at the end and that will turn our code block into comment. Then that way, the computer skips the code and doesn't execute it. OK, next, we need to check the Raycast every fame and to check every frame, we need to do this every time in the Update Function. So, let's put our Update Function back into the script and inside this function, let redefine the hit variable once again. But this time, let's make it of the type RaycastHit, which is another variable type to find in Unity 3D already. Alright, next, we need to cast our Raycast and this will do in an if statement, so that we make sure that it's checked every single frame. Now, our if statement will need the Raycast, so let's start adding our Raycast here by typing Physics, because Raycasts are derived from the Physics Engine, .Raycast and this function needs a couple of variables; 4 to be exact and the first one is the position where we're casting the ray from and that is of course, the position of our player and this is already attached to our player, so we can simply type, transform.position. Next, we need to know in what direction we're casting the ray and it's of course the forward direction or z direction. So shorthand for z is transform.forward, and then we need to know where to save our data in and this will be our hit variable that we defined right before this if statement and then last but not least, it's want's to know the length of our Raycast and we'll make that 5 units and in Unity, you could think that would be exactly 5 meters. OK. Now, we have our Raycast and this is checked every single frame. Let's add some comments to this; check if we're colliding and then next, we need to add some other quote that will make sure if we're colliding with the doors. So, let's add another comment, with the door, or in this case, a door because, we can have multiple doors with the same tag. Alright, of course, we need another if statement here to check with our hit variable was holding out for us. So, we need to type hit.collider.gameObject.tag equals door. Now, notice, that here with the other collision functions, we've checked hit.gameObject straightaway. However, when our hit variable is a RaycastHit and not a ControllerColliderHit, then we need to add the collider first, because we're actually talking to the collider. This already knows what the collider is. So, we need to hit.collider.gameObject.tag, otherwise, we'll get an error. Alright, next, we already know that we're colliding with the door, now, let's open the door. It says to me open right here. Alright, then that goes hit.collider.gameObject and you've guessed it, now, we need to play the animation, animation.Play, an animation we're playing of course is door underscore open. Now, let's save this script. Switch back to Unity and we don't see any errors popping up up here so, I guess our code is at least, syntax-wise, correct. Now, when I run my game, let's walk up to the door and as you can see, it opens right 5 meters before I actually hit it. Really nice. Really good. OK, I'm pretty happy with this. If you want, you can increase the number of course, you can change into a variable, for example, say, Raycast or variable RaycastLength equals 5 and then here, instead of the 5, you just use the RaycastLength variable and then it will show up in the editor here. If it refreshes, then you save this one again, the cast is 5, go back into our player and then sometimes it happens in Unity, that when you save a script, it doesn't update and it helps to just restart Unity, but of course, you can imagine that down here, the RaycastLength will show up on the bottom and you can change the length of it to tweak parameters while you're playing the game. Now, also, if you get an error like I'm having down here, you can double click it and it will bring up the Console window, as well as the script and the Console window will tell you exactly what's wrong, and it says that I have an unknown identifier RaycastLength, which is this one, and I probably misspelled it. So, let me copy it again and then paste it, save. Let's go back into our script and see if it works when I run the game and it does. And now as you can see, the variable does show up down here. It sometimes help when you run the game and stop the game to refresh and of course, now I can change, for example, to 7, test again and the door will open a lot sooner before I actually reach the door. So, there you have it. That's how Raycasting works. Let's move on and do some more exciting stuff.

Tutorial Information

Course: Introduction to Game Development Using Unity 3D
Author: Efraim Meulenberg
SKU: 34092
ISBN: 1-936334-03-8
Release Date: 2010-02-23
Duration: 5.5 hrs / 75 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available