B1 中級 英國腔 3137 分類 收藏
開始影片後,點擊或框選字幕可以立即查詢單字
字庫載入中…
回報字幕錯誤
Raycasting is the process of shooting
an invisible ray from a point, in a
specified direction to detect whether any
colliders lay in the path of the ray.
One such example of this would be
shooting a gun. In this instance our
character wants to shoot the evil box that
betrayed him and killed his father.
The syntax of the raycast function looks like this.
It can be confusing at first
but once you understand what each part does
it makes much more sense.
Firstly, the origin of the ray is a
point in world space.
So in this instance we'd choose a point in
front of the barrel of the gun, stored
as a Vector3, an X, Y and Z position.
However, because our world coordinates
direction won't be facing in the direction
we're shooting we will need a second Vector3
to store our direction in.
These two Vector3 variables make up
our ray. But we can also substitute
in a Ray variable, as this data type
can store two Vector3's.
Our code would then look like this.
The next argument in the function is a
RaycastHit variable that stores
information on the colliders hit.
So that it can be queried in code as to which
objects are intersected by the ray.
Finally there are two optional arguments,
Distance, which defines the length
of the ray, if omitted the ray will default
to an infinite length.
And Layer Mask. This is the number
of a particular layer in Unity's layer system
on which you can place objects if you
wish to make the ray ignore them.
Let's look at another practical example of
using raycasting.
In this example we have a parachute crate
that opens a parachute when it's
nearing the floor.
The crate is made up of two parts,
the chute and the crate itself.
The chute has two animations
one to open the chute
and another to close it.
In this example we need to cast a ray
downwards in order to see how far the crate is
from the floor, and we check for the
floor by looking for the environment collider.
Our collider for the environment is tagged
with the word environment.
And in our script we are looking for that tag.
The RayCast function gets placed inside
an IF statement so that if it returns true,
meaning if it intersects with anything,
then the comments within the IF statement
will be carried out and the RayCastHit
variable can be queried as to what has been hit.
So within an IF statement we've written
Physics.Raycast, we have a landingRay variable
that's storing the position of the box
and a downward direction. We're using
the shortcut Vector3.down,
and we're using this as the ray to cast.
Our RaycastHit variable - 'hit' -
is storing anything that gets hit by the
ray as it is cast downwards,
and the distance, or 'length' or the ray
is defined by our 'deployment height' variable.
If the ray intersects with a collider
then we call the deploy parachute function.
This function then simply sets our Boolean
'deployed' flag to true so that this cannot repeat.
And then we set the drag of the rigid body
to the variable 'parachuteEffectiveness'.
So we slow down the crate as if it's being
held up by the parachute.
We also play the animation
on the parachute object,
which is a game object that we'll assign
to the public variable.
We then have a separate OnCollisionEnter function
which simply plays the closing animation.
So we know that as soon as it hits the ground
or another object the parachute can close.
So here we've set the length of the ray to 4
by setting 4 as our deployment height
And we're setting the drag of the rigidbody to 8
by setting the parachute effectiveness to 8.
And we've simply dragged our parachute
chute object on to the parachute variable.
Because this is the object that has an animation
component in order to playback
it's opening and closing animations.
So let's see that play one more time.
It's also worth keeping in mind
that although you cannot see
raycasts drawn in the scene view
or in the game. You can also use the
Debug.DrawRay function
in order to preview where a ray would be going.
By adding Debug.DrawRay
we're drawing a visual ray from
the position of the box in the direction
of Vector3.down, multiplied by
the deployment height - the length of our existing ray.
And by doing this we've matched the actual
ray that we're casting in the IF statement below.
So when we play this back you can see that
Unity demonstrates the ray
by showing us the drawn ray in the scene view.
提示:點選文章或是影片下面的字幕單字,可以直接快速翻譯喔!

載入中…

光线投射-Unity 官方教程 (Raycasting - Unity Official Tutorials)

3137 分類 收藏
朱瑛 發佈於 2014 年 5 月 2 日
看更多推薦影片
  1. 1. 單字查詢

    在字幕上選取單字即可即時查詢單字喔!

  2. 2. 單句重複播放

    可重複聽取一句單句,加強聽力!

  3. 3. 使用快速鍵

    使用影片快速鍵,讓學習更有效率!

  4. 4. 關閉語言字幕

    進階版練習可關閉字幕純聽英文哦!

  5. 5. 內嵌播放器

    可以將英文字幕學習播放器內嵌到部落格等地方喔

  6. 6. 展開播放器

    可隱藏右方全文及字典欄位,觀看影片更舒適!

  1. 英文聽力測驗

    挑戰字幕英文聽力測驗!

  1. 點擊展開筆記本讓你看的更舒服

  1. UrbanDictionary 俚語字典整合查詢。一般字典查詢不到你滿意的解譯,不妨使用「俚語字典」,或許會讓你有滿意的答案喔