WEBVTT

1
00:00:06.139 --> 00:00:08.359 A:middle L:90%
Hi and welcome back to the Unity 3-D third

2
00:00:08.359 --> 00:00:10.980 A:middle L:90%
person camera tutorial series. In the last video we

3
00:00:10.980 --> 00:00:14.410 A:middle L:90%
set up a new behavior where the camera has the

4
00:00:14.410 --> 00:00:16.829 A:middle L:90%
character orbit around it which was pretty different and the

5
00:00:16.829 --> 00:00:19.730 A:middle L:90%
character actually rotates as we run around the camera.

6
00:00:19.820 --> 00:00:23.879 A:middle L:90%
We talked about translating the coordinates from the control stick.

7
00:00:23.890 --> 00:00:28.850 A:middle L:90%
Let me zoom out a second. We

8
00:00:28.850 --> 00:00:30.870 A:middle L:90%
talked about translating the control state coordinates, which you

9
00:00:30.870 --> 00:00:33.570 A:middle L:90%
can see in blue in this image into world space,

10
00:00:33.570 --> 00:00:36.539 A:middle L:90%
which is the green vector and then taking the

11
00:00:36.539 --> 00:00:39.030 A:middle L:90%
cross product, which is the red line here in

12
00:00:39.030 --> 00:00:42.200 A:middle L:90%
the play view. Using that to determine the direction

13
00:00:42.200 --> 00:00:44.939 A:middle L:90%
that the character needs to turn. That's that was

14
00:00:44.939 --> 00:00:46.899 A:middle L:90%
the last lesson. So in this lesson we're going

15
00:00:46.899 --> 00:00:48.869 A:middle L:90%
to see how we can polish the script up because

16
00:00:48.869 --> 00:00:50.549 A:middle L:90%
it's kind of got a problem. If we decided

17
00:00:50.549 --> 00:00:55.350 A:middle L:90%
to run towards the wall and we run into it

18
00:00:56.740 --> 00:00:58.420 A:middle L:90%
we can kind of see around the world. It

19
00:00:58.420 --> 00:01:00.189 A:middle L:90%
just looks sort of strange and it's not exactly what

20
00:01:00.200 --> 00:01:03.100 A:middle L:90%
we want, because you can get into this weird

21
00:01:03.100 --> 00:01:04.500 A:middle L:90%
situation where we're looking around walls instead, what we

22
00:01:04.500 --> 00:01:08.700 A:middle L:90%
want to do is sort of have the camera push

23
00:01:08.700 --> 00:01:11.159 A:middle L:90%
up against the wall when we run into it,

24
00:01:11.640 --> 00:01:12.260 A:middle L:90%
so we're going to implement that in this lesson.

25
00:01:12.739 --> 00:01:15.750 A:middle L:90%
Mhm. Okay, let's get started, let's go

26
00:01:15.750 --> 00:01:18.879 A:middle L:90%
into our script file, we're only going to need

27
00:01:18.879 --> 00:01:21.480 A:middle L:90%
to edit one file for this, and that's going

28
00:01:21.480 --> 00:01:25.140 A:middle L:90%
to be the third person camera file. So we

29
00:01:25.140 --> 00:01:26.200 A:middle L:90%
want to do is you want to go and uh

30
00:01:26.209 --> 00:01:30.069 A:middle L:90%
before this lesson, I actually remove that offset variable

31
00:01:30.069 --> 00:01:30.939 A:middle L:90%
that we used to have, and now I just

32
00:01:30.939 --> 00:01:34.709 A:middle L:90%
calculate uh the character offset just using using code,

33
00:01:34.719 --> 00:01:38.420 A:middle L:90%
which is quite a bit better. Um So we

34
00:01:38.420 --> 00:01:38.730 A:middle L:90%
want to do now is you want to make this

35
00:01:38.730 --> 00:01:42.299 A:middle L:90%
new method called compensate for walls. And what this

36
00:01:42.299 --> 00:01:46.049 A:middle L:90%
is going to do is going to move the camera

37
00:01:46.049 --> 00:01:47.769 A:middle L:90%
back to collide with the wall. We're going to

38
00:01:47.780 --> 00:01:49.680 A:middle L:90%
use a Raycast to do that. So we

39
00:01:49.680 --> 00:01:53.230 A:middle L:90%
got this method compensate for walls. We've got a from

40
00:01:53.230 --> 00:01:55.730 A:middle L:90%
and a to. We're going to draw a line

41
00:01:55.739 --> 00:01:57.700 A:middle L:90%
from the object to the target and you'll be able

42
00:01:57.700 --> 00:02:00.370 A:middle L:90%
to see that in science. So let's have a

43
00:02:00.370 --> 00:02:01.219 A:middle L:90%
look at that real fast. And let's add a

44
00:02:01.219 --> 00:02:06.969 A:middle L:90%
call to this in our method right before smooth position.

45
00:02:07.340 --> 00:02:15.330 A:middle L:90%
So let's see what this does. Okay,

46
00:02:15.330 --> 00:02:19.539 A:middle L:90%
As you can see in the b right here,

47
00:02:19.550 --> 00:02:23.409 A:middle L:90%
we have in our scene view a sigh in vector.

48
00:02:23.409 --> 00:02:24.159 A:middle L:90%
Now going from the camera to the character.

49
00:02:24.840 --> 00:02:27.430 A:middle L:90%
Uh, and that's the target where we're trying to

50
00:02:27.430 --> 00:02:29.349 A:middle L:90%
aim at, which is 1.5 m up from the

51
00:02:29.349 --> 00:02:30.150 A:middle L:90%
top of the character. So if we run into

52
00:02:30.150 --> 00:02:39.979 A:middle L:90%
a wall now, if you turn around and you

53
00:02:39.979 --> 00:02:45.030 A:middle L:90%
can see that vector is colliding with the wall.

54
00:02:45.039 --> 00:02:46.530 A:middle L:90%
And if you're looking at the code, there's a

55
00:02:46.539 --> 00:02:51.719 A:middle L:90%
ray cast right here where we're doing great cast from

56
00:02:51.729 --> 00:02:53.629 A:middle L:90%
the object to the target and stored in this wall.

57
00:02:53.629 --> 00:02:54.849 A:middle L:90%
Hit objects. So we just create a new

58
00:02:54.860 --> 00:02:59.250 A:middle L:90%
recast hit object called wall hit. And we can

59
00:02:59.250 --> 00:03:01.400 A:middle L:90%
draw a ray. And they were doing this red

60
00:03:01.400 --> 00:03:04.389 A:middle L:90%
ray here, which you can see in this view

61
00:03:04.400 --> 00:03:07.889 A:middle L:90%
museum in on it, you can see this red

62
00:03:07.889 --> 00:03:09.719 A:middle L:90%
ray where we hit the wall. So I'm just

63
00:03:09.719 --> 00:03:13.939 A:middle L:90%
drawing using vector dot left. Just a red vector

64
00:03:13.939 --> 00:03:15.199 A:middle L:90%
where it hits the wall. So you can see

65
00:03:15.199 --> 00:03:17.280 A:middle L:90%
we've got a correct value by taking this this direction

66
00:03:17.289 --> 00:03:21.379 A:middle L:90%
from this line right here where we go from the

67
00:03:21.389 --> 00:03:23.680 A:middle L:90%
from the target. I'm sorry, from the object

68
00:03:23.689 --> 00:03:24.870 A:middle L:90%
to the target, we hit the wall, we

69
00:03:24.870 --> 00:03:27.830 A:middle L:90%
draw back to where we hit, and now let's

70
00:03:27.830 --> 00:03:30.039 A:middle L:90%
move the camera to the X and Z coordinates while

71
00:03:30.039 --> 00:03:32.979 A:middle L:90%
maintaining the Y value of the camera. So we're

72
00:03:32.979 --> 00:03:37.520 A:middle L:90%
passing the camera's target in by reference and what this

73
00:03:37.520 --> 00:03:39.020 A:middle L:90%
means. We can actually change this value and we

74
00:03:39.020 --> 00:03:40.860 A:middle L:90%
don't have to actually store into a new value up

75
00:03:40.860 --> 00:03:45.520 A:middle L:90%
here. So if the wall hit happens, we

76
00:03:45.520 --> 00:03:49.349 A:middle L:90%
just move it, I we just moved the target

77
00:03:50.139 --> 00:03:53.710 A:middle L:90%
to the wall, point X and Z and the

78
00:03:53.719 --> 00:03:57.400 A:middle L:90%
Y value of the target. So we don't actually

79
00:03:57.400 --> 00:03:58.900 A:middle L:90%
change the Y value. And that's why the cameras

80
00:03:58.900 --> 00:04:00.349 A:middle L:90%
still here, as you can see, it's at

81
00:04:00.349 --> 00:04:01.500 A:middle L:90%
the X and Z values of this point, but

82
00:04:01.500 --> 00:04:04.159 A:middle L:90%
it maintained its wipe out. So that's helpful there,

83
00:04:04.939 --> 00:04:06.870 A:middle L:90%
and that's basically how it works. So that's

84
00:04:08.099 --> 00:04:10.620 A:middle L:90%
that's all there is to that, it's not quite

85
00:04:10.620 --> 00:04:13.360 A:middle L:90%
as robust as the one in uh and the Zelda

86
00:04:13.360 --> 00:04:15.599 A:middle L:90%
camera, just because you can get into situations like

87
00:04:15.599 --> 00:04:17.350 A:middle L:90%
this where you can actually see outside of the world

88
00:04:17.350 --> 00:04:20.399 A:middle L:90%
still, but it's definitely a step above what we

89
00:04:20.399 --> 00:04:24.639 A:middle L:90%
had before. So that's it for this lesson and

90
00:04:24.639 --> 00:04:25.850 A:middle L:90%
uh, tune in next time.

