22

When I had this problem while working on my Cubes, I found the paper "A Fast Voxel Traversal Algorithm for Ray Tracing" by John Amanatides and Andrew Woo, 1987 which describes an algorithm which can be applied to this task; it is accurate and needs only one loop iteration per voxel intersected. I have written an implementation of the relevant parts of the ...


20

What is a raycast? What are blocking objects and blocking mask? A ray is a mathematical device that starts at an origin point and continues on in a specific direction forever. With a raycast you're casting a ray, cast being used like the word throw. It's like if you threw a rock and it continued on in that direction forever, it wouldn't stop until it hit ...


13

Amit Patel has written a very nice article on 2D ray casting. This involves casting rays to each of the vertices inside the range of the light source to build a light mesh. All of the visual examples are interactive in the post and very easy to understand. You don't have to limit yourself to a box either, the perimeter you trace can be any shape you like....


10

Break movement into two steps Select a position on a 2D X/Z plane located at the entity's current Y position. Set the offset (up or down) from that plane. This is similar to how Homeworld, one of the first 3D space RTS games handled movement. Works really well. Breaking movement into two steps provides the most control, simply because the mouse is a 2D ...


9

Transforming the ray position and direction by the inverse model transformation is correct. However, many ray-intersection routines assume that the ray direction is a unit vector. If the model transformation involves scaling, the ray direction won't be a unit vector afterward, and should likely be renormalized. However, the distance along the ray returned ...


8

It seems you include a square when you can see any point in it while the eye is at the single fixed point. However, you should only include a square when you can see the middle of it (assuming the "eye" is also in the middle). Or you can draw rays from each corner of the starting square to the corners of the tested square, if you can see any from any then ...


8

Eric Lippert wrote a series of blog posts about using "shadow arcs" to only iterate over visible cells in linear time without having to search for obstacles in each cell's visibility check. The blog series is archived, but you can find some of it at Microsoft's MSDN blog archive or check the Internet Archive. I've adapted the scanning logic and ...


7

I have implemented the algorithm suggested by Jimmy. Video of the code in action here: https://youtu.be/lIlPfwlcbHo /* What this code does: Rasterizes a single Field Of View octant on a grid, similar to the way FOV / shadowcasting is implemented in some roguelikes. Clips to bitmap Steps on pixel centers Optional ...


7

Eric Lippert wrote an excellent series on generating line-of-sight in C# with Shadow Casting on a rectangular planar grid.. Amongst other issues, Eric dealt with various questions that must be answered about the line-of-sight requirements, which give different results, and gives examples of a couple of different results. One of the articles deals in depth ...


7

You need to use the range instead of the distance when you cast the ray. The range is the length of the projection of the ray onto the camera plane. See this source. Though IMHO some amount of fisheye distortion is kind of cool to keep around.


7

There is a (optional) parameter for that in Physics.RayCast which is available in Unity 5.2 and up: public bool RaycastNoTriggers(Vector3 startPosition , Vector3 direction, float distance) { Ray ray = new Ray(startPosition, direction); RaycastHit hit; return Physics.Raycast(ray, out hit, distance, DefaultRaycastLayers, QueryTriggerInteraction....


7

Pixel picking is effective if you need to do lots of raycasts from one source. You bear the overhead of rendering and readback once, then get all your many picks at low incremental cost each. To get just one ray result you've already had to calculate thousands or millions whether you use them or not. If you're only doing a few raycasts at a time (say a ...


6

Real Time Collision Detection does indeed have this information - look at section '5.3.2 Intersecting Ray or Segment Against Sphere', page 178/179 in my copy. I'm not sure if it is okay to reproduce the code but I found many copies of it online (google books, for example) - here it is verbatim from Real Time Collision Detection: UPDATE: The following ...


5

You can calculate the ray from camera for any pixel px,py with the following formula: ray.origin = camera.getPosition(); relativeX = px * 2 / screenWidth - 1; // [0, screenWidth] -> [-1, 1] relativeY = py * 2 / screenHeight - 1; // [0, screenHeight] -> [-1, 1], might need to be flipped depending on the coordinate system ray.direction = vector(...


5

Writing up an answer from my comments as I believe it's not such a crazy idea after all. Assumptions The "Up" direction should not matter in these calculations You have found the object in focus and you can ray cast from its front just like you did with the character The "Up" direction is Z. (This could be Y depending on who you are talking to or what ...


5

I've had this happen with the 3D raycast in the past, and it seems to be because the raycast functions have so darned many optional parameters, and LayerMasks aren't picky about what type they're treated as. From the docs: public static RaycastHit2D Raycast( Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, ...


5

It's called a flood fill, and it's what you see in paint programs. It's very fast. Pseudocode: declare visited list //the results you want declare unvisited list add current element (where red dot is) to unvisited list while unvisited list not empty get current element from unvisited list add current element to visited list for all (8) neighbours ...


5

I'd recommend testing empirically to confirm, but the second answer at this link says that triangle data for submeshes is indexed sequentially in order of submesh index. That means all the triangles for submesh 0 have lower indices than those in submesh 1, etc... So, you can iterate over the submeshes until you find the one your triangle belongs within: ...


5

Physics.OverlapSphere() and Physics2D.OverlapCircleAll() will return all colliders within a radius of a point. Those colliders are in range, but possibly not in view. If you are going to do this often, create an array once, and pass it to Physics2D.OverlapCircleNonAlloc(), instead. To see if they are in view, check the dot-product of the normalized collider-...


5

You could use a Dictionary. Store all Transforms as a key with reference to each Car. In this example, we have a god Game class that holds references to all Cars. (just make sure your Script Execution Order has Game execute before Car does) Each Car class adds itself to the Game's Car Dictionary upon Awake. Then in your Raycast, you get the Car directly ...


5

SphereCast methods take a sphere and slide it along a line to see where it hits objects along its travel. The radius parameter is the size of the sphere. The maxDistance parameter is how far the sphere should travel in the given direction. To simply check for objects near your player, you want CheckSphere or OverlapSphere / OverlapSphereNonAlloc instead. ...


4

I think this is what you want: Physics.SphereCast From the docs: Casts a sphere against all colliders in the scene and returns detailed information on what was hit. This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere ...


4

Your problem is not the ray cast, but that your grid cell doesn't reflect its actual state or in other words it might contain objects that it doesn't know about. When a certain object intersects a grid cell, the grid cell should know about it, this way when an object intersect multiple grid cells each grid cell should have a reference to that object, this ...


4

The physics raycast is a different system and doesn't see UI elements. So you can not cause the UI to block physics raycasts. However you have options: What you can do is cast two rays. One raycast for your UI elements and one for your physics objects. If your UI raycast detects a UI element, you know a UI element is in the way, and you shouldn't cast your ...


4

I understand that you are at a loss. I also liked to hack pixels when I was young, and did some games on calculator devices, or dos era this way. This can still be done today, but, because of various hardware history of evolutions, it has become more involved. CPU pixel pushing Notably, we can still push pixels one by one, but it would be incredibly slow ...


4

This is because your raycast is from the center of the player to a length equal to speed. You're discounting the size of the player in this calculation. hit = Physics2D.Raycast(body.position, Vector2.right * moveDir.x + player_radius, 1); Where player_radius is the size of your player. It would be best to make this either a reference to the game object's ...


4

Breaking movement in two steps is an outdated approach. Mouse movement for the horizontal plane. Mouse wheel for vertical plane. Homeworld did a fine job, but mouse wheels weren't so widely deployed yet so they didn't go with that. You can still have modifier keys to switch mouse movement between the modes but moving the mouse and scrolling can be done ...


4

Ok. After some experiments I ended up replacing the EdgeCollider2D with a PolygonCollider2D. I generated the collider points (taking the width of the wire into account) by computing the Euclidean distance between two consecutive points on the curve (you need to iterate through all the points) and the perpendicular on the distance vector (using the cross ...


4

First, configure your Layers so that you have a layer for bullets and a layer for enemies. Ensure you've set the bullet prefabs to use the bullet layer, and the enemies/enemy prefabs to use the enemies layer. Next, you can apply your hovering damage using a script like this one. Main things to note about this strategy: We check for objects overlapping the ...


4

I am assuming you have a sphere of radius r between two timeframes, at position $$x(t), \,\,t \in \left[0, 1\right]dt$$ and want to know whether there exists a time t such that the sphere is touching the wall. A wall is a plane, in general. So it can be defined using a position vector and the normal versor $$ x_p,\,\,\widehat{n} $$ See figure. We assume ...


Only top voted, non community-wiki answers of a minimum length are eligible