Welcome Unity Developers, Back with another Blog. Today we are Going to learn about: "Physics.Raycast"

Physics.Raycast is normally used to draw a Ray of n Distance, and if the Ray hits anything It let us Know what it Hits (Make sure the other object has collider attach to it otherwise raycast will pass through object without detecting it). Physics.Raycast is used in Games whenever we want to hit something from distance or want to check whether our gameobject will hit something in future if it goes straight.
Note that Raycast is always cast In a straight line from the center of the gameobject that is casting the ray.

So we can make something like:

  • AI of a character which checks while moving whether there is any object which he may collide with say in range of 10 units of unity, if it keeps on moving in that direction
  • A gun Which shoots when a input is given and if it hits something say player, Player health decreases.
  • Can Create a logic if we fire on players and the player is on same team than he was not Hit by Bullets.
As concluded one can make many logic's with the help of Raycast. So let's see How To Use One.

Step 1: Scene Setup

1. Make a plane and add a cube, name it Player and add a Script to it with any name you like to have.
I named mine:"PlayerRaycaster"
Drag the camera and make it child of your player  to have  a fancy Player view.

2. Add Different Types of objects in your scene at some distance to your Player.

Your Scene will look something like this bad if you also don't have Models to put.



(HaHaHaHa Inner Voice: Have to have a modeler  friend to put some cool models in Blogs).

Step 2: Scripting

1. Will write some few lines of code to move around our player in scene.



In this all we are doing is Making two variables HorizontalInput, VerticalInput  to assign are inputs from the Keyboard To our player,
Next we create two more variables to determine each axis speed, Than we move are player using Line no.22 by translating it and adding the keyboard input multiplying by speed and time.
Same with Rotation.

2. Now let's add Physics.Raycast to over player.
Add these line to our existing script.



Well this lines are copied from unity scripting Reference :-)
What this peace of code will do is Cast a ray from over gameobject and if the ray hits any object the ray will colored as yellow and debug in console "Did Hit"
else the ray will be colored as white and debug in console "Did not Hit"
Physics.Raycast can takes up-to five parameters which are:



1. Origin is the gameobject you want the raycast to start from.

2. Direction is the direction you want your raycsat to go from center of your object.

3. maxDistance is the range you want your ray to go(mathf.infinty will let it go to infinity).

Other than that we used out Hit
which tells what we Hit.

Step 3. Run Your Game

You can see whenever your player ray hits and other object the ray itself turns yellow and debug in console "Did Hit"
and when not hitting any other object it turns out to white and debug in console "Did not Hit"
Let's do a simple change in our script and add a few line of code.



Now when we run the game we have our own Law of attraction.

If you Want to learn about How to Rayacst On UI. Please check my other blog Graphical Raycaster.
Link:Graphical Raycaster In Unity


This Blog is Open to edits and feedback.
Until Than Happy Codiiing!!!

Axact

İslevselBilgi

Emre Oy Tarafından Tasarlanmıstır

Post A Comment:

0 comments: