Int value helps you to tag what will be recognized. In your Unity 2D game, for the object you are setting a ray for could ignore some specified objects while hitting the others. Int layerMask value is worth talking about, even if it’s not necessary. Again, it’s a Vector2 to give our ray a dimension to travel in, different than the dimension of our point of origin. Vector2 direction determines the direction of our ray. This code is specific to Unity 2D games, all vectors will be handled in Vector2 because of the two dimensions of your game. This point is stored as a Vector2, with X & Y positions. Vector2 origin defines the point of origin for your ray. While defining a ray for your Unity 2D game is very similar to doing so for Unity 3D, though the syntax has some minor changes: The syntax is very similar to Unity 3D Raycast’s. Transform.position = Vector3.MoveTowards(transform.position, move, speed) (define the move and speed variables to move the object) Raycasting for Unity 2D games Move.z = (I don’t want our ray to travel in Z dimension.) Move.y = (I don’t want our ray to travel in Y dimension) Vector3 move = hit point (I defined a new variable since I’m not able to modify the hit point) When the if condition is met, I do the following: The if function following – if (Physics.Raycast(ray,out hit,100))– checks if our ray hits something at a distance of 100. RaycastHit checks what happens when our ray collides with something on its way. This is the trick of casting rays from the camera. We create our ray thanks to ScreenPointToRay. Ray ray= camera.ScreenPointToRay(mousePos)) is the beauty here. I defined the as mousePos.z, to add some depth. Our Input.mousePosition will be defined as a Vector3 for obvious reasons. MoveWithRay()represents the movement of our ray. the if (Input.GetButton(“Fire1”)) part is setting an if function which is triggers -pun intended- our ray if I pressed the “Fire” button. Nevertheless, let’s break it down:Ĭamera = Camera.main defines our main camera variable. Comments are put into the code for you to use as a guide. Here’s an example text for setting a ray from the camera/cursor: Raycasting with mouse position is easy!ĭon’t worry, your guy is here to explain. Let’s continue with the example of writing a script for firing a gun. All you need to do is use Input.mousePosition, this transports your position of mouse as input for you to use as your origin of raycast. Our mouse position is a property of the input class. This will help us especially if we are working on a Unity 3D game.įirst, you need to convert your mouse position to the world of Unity. Setting the mouse position as a point of origin is an easy and effective way to determine where your ray originates from. There are different ways to define the origin of our ray. In your Unity 3D game, for the object, you are setting a ray for could ignore some specified objects while hitting the others. Again, it’s a Vector3 to give our ray a dimension to travel in, different than the dimension of our point of origin.įloat distance is the distance that our ray should travel from the point of origin in the direction we determined, as a float value. Vector3 direction determines the direction of our ray. This code is specific to Unity 3D games, all vectors will be handled in Vector3 because of the three dimensions of your game. This point is stored as a Vector3, with an X, Y & Z position. Vector3 origin defines the point of origin for your ray. For 3D games in Unity the syntax of ray function looks like this: A basic code for Raycasting in Unity 3D.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |