290

Share

GAME DEVELOPMENT

How To Use Ray Cast In Unity 3D?

  • Home
  • Blogs
  • How To Use Ray Cast In Unity 3D?

Raycast is the physics function of Unity. It projects a ray into the scene of a game or design, returning a Boolean value if a target was successfully achieved.

Agnesh Pipaliya

July 18, 2022

Raycast is a function that creates a connection between objects in Unity 3D. It represents a point of origin and a direction for the ray to travel.

There are several features available in Unity to use the Raycast function in a design. It is used for many purposes in 2D and 3D games. In this blog, we will see what Raycast is and how it can be used in games.

Ray Casting

"Ray Casting" is a feature that is provided by Unity, which is used in video game development for things such as determining where a projectile will go, making lasers, and much more.

Initially, a ray is one type of ray that gets sent out from a position in 2D or 3D space and moves in a specific direction.

The Unity 3D engine has built-in functions that can be used to implement a Raycast in our game.

Steps to Find Gameobject using Raycast

In this blog, I will be giving examples of how to implement a Raycast in Unity 3D and how to use the Raycast information to determine if an object is in line of sight of the character and if we can interact with that object.

To understand how raycast works, First of all, I’m going to assume that you already have a scene set up with a character controller of some kind that you can use. If you don’t, then go ahead and create a new scene, create some kind of plane you can walk around on, and put a character controller in the scene. If you don’t have a controller, then follow the below-mentioned steps.

Now In My Case I Have Been Made Scene Set Up With Following Steps:

Step 1

Create a new scene with any name that you want.

Ex.- RayCast

Step 2

Goto hierarchy and create a 3D plane, apply some kind of material so it becomes visible and set its transform as shown in the figure.







Step 3

Create Cube and apply some kind of material so it becomes visible and set its transform as shown below figure.






Step 4

Create a Capsule and apply some kind of material so it becomes visible and set its transform as shown below figure.






Step 5

Apply some kind of animation so that the Capsule can move between two point continuously on X-axis, here you can also apply script on Capsule for movement.




Step 6

Now create a new C# script and apply the below code on that script. Assign this script on your character that you want to fire rays from it, in my case I’m just assigning this script on Cube.

You make sure that the game object that you want to find by ray cast has a collider component because finding a game object by ray cast has required collider to find collision detection between that collider and ray.




As shown above code, here the transform.position is a Cube position which we can pass as a start position for hit emission and we pass a 40 value as a distance parameter of DetectHit function,so ray which emitted from start position should check for collisions within this distance, or we can also call it is a max distance that rays should check for collisions.

Note that if you want to check collision detection with the infinite distance you should set distance as Mathf.Infinity

Step 7

Now run the scene and you can see the green color rays are emitted from Cube and go in forward direction within its max distance limit.




You can also see when capsule collider is triggered at that time rays are not pass through capsule collider and as shown in the above code we have received the message as a log in our console-like “There is something in front of the object!......game object name....= Capsule”.






Step 8

So we finally received a game object that collides with ray by its name, but you can also find that game object with tag by assigning tag of that game object.

Step 9

Now you can able to do what you want with this game object which is collided by ray. You can do something like destroying that game object, apply force, apply rotation and much more.

NOTE: If you have a scene with a character controller, you no need to do all kinds of this stuff. You can need to just apply this type of code on your character that you want to fire a ray from it and apply collider on that game object that you want to find by raycast.

Conclusion

The Android SDK offers various mechanisms for the implementation of audio recording and playback. But if you do not know the process of audio implementation, your application's user engagement will be affected.

Hence, you should learn the process of recording audio programmatically in an Android application.

img

Agnesh Pipaliya

COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Leave a Comment