Go where I'm looking
In this next script, instead of being random, we'll send Ethan to wherever we look. In Unity, this is accomplished by using ray casting. It's like shooting a ray from the camera and seeing what it hits (for more information, visit http://docs.unity3d.com/Manual/CameraRays.html).
We're going to create a new script, which will be attached to WalkTarget
like before, as follows:
- Select the
WalkTarget
object in theHierarchy
panel or theScene
view. - In its
Inspector
panel, click on theAdd Component
button. - Select
New Script
. - Name it
LookMoveTo
. - Ensure that the
C Sharp
language is selected. - Click on
Create
andAdd
.
This should create a script component on the WalkTarget
object. Double-click on it to open it in your code editor.
Note
This LookMoveTo
script replaces the RandomPosition
one we created before. Disable the RandomPosition
component of WalkTarget
before proceeding.
The LookMoveTo script
In our script, each time Update()
is called, we'll read where the camera is pointing (by...