10 votes

How to handle RTS Click-And-Move in 3D Space?

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 ...
House's user avatar
  • 73.2k
8 votes
Accepted

Pixel Picking vs Raycasting

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 ...
DMGregory's user avatar
  • 133k
8 votes

More efficient way to implement Line of sight on a 2d grid with ray casting?

I have implemented the algorithm suggested by Jimmy. Video of the code in action here: https://youtu.be/lIlPfwlcbHo ...
Stoiko's user avatar
  • 99
8 votes
Accepted

Difference between 'max distance' and 'radius' in Physics.SphereCastAll

SphereCast methods take a sphere and slide it along a line to see where it hits objects along its travel. The radius parameter ...
DMGregory's user avatar
  • 133k
6 votes
Accepted

What is a faster alternative to a GetComponent from a RaycastHit?

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 ...
Chris McFarland's user avatar
6 votes
Accepted

How to raycast down to the floor plane to determine world space coordinates in Godot

I'm not sure I understand your limitations, so I will give you a few approaches, and hopefully this gets you to the solution along the way. This is not an exhaustive list. Pickable Object If you have ...
Theraot's user avatar
  • 26.1k
5 votes
Accepted

How to find a material through a raycast in Unity 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 ...
DMGregory's user avatar
  • 133k
5 votes

Need help with a field of view-like collision detector!

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,...
Jon's user avatar
  • 3,684
4 votes
Accepted

How to handle RTS Click-And-Move in 3D Space?

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 ...
helena4's user avatar
  • 438
4 votes

2d raycast collision problem

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. ...
Draco18s no longer trusts SE's user avatar
4 votes

How can I correct an unwanted fisheye effect when drawing a scene with raycasting?

It looks like you're firing out your rays at evenly spaced angular intervals. ...
DMGregory's user avatar
  • 133k
4 votes

How to detect when the mouse is over a particular enemy collider, without falsely selecting bullet colliders?

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 ...
DMGregory's user avatar
  • 133k
4 votes

Efficient collision detection of sphere with wall

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 ...
Turms's user avatar
  • 408
4 votes
Accepted

What is a raycast?

A raycast is like shining a laser pointer in your scene, to see what the laser hits. It takes a starting point and a direction as input (together, they make a "ray"). The physics engine takes this ...
DMGregory's user avatar
  • 133k
4 votes

How can I offset a direction when raycasting?

The X/Y Problem strikes again! I'm trying to figure out how I can best offset a direction..I want the direction to always be slightly offset to the right of the object from the mouse position? And ...
DMGregory's user avatar
  • 133k
3 votes

How can I fix the "fisheye" distortion in my raycast renderer?

Typically, you wouldn't space your rays at even angles. This is the source of the distortion. See this answer for details: https://gamedev.stackexchange.com/a/156853
Kody King's user avatar
3 votes

Need help with a field of view-like collision detector!

First of all... don't over complicate the objective... I would suggest creating your own collider, and not using one from unity... First of all, get all of the enemies and put them in their own ...
RayBan's user avatar
  • 31
3 votes

Finding which tiles are intersected by a line, without looping through all of them or skipping any

The code in the example that the accepted answer links to needs some adjustment for perfectly diagonal lines. Here's a complete demo application written with Qt (C++ and QML). Relevant C++ code: <...
Mitch's user avatar
  • 295
3 votes
Accepted

What type of Collider should I add to a LineRenderer?

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 ...
Horia Jurcut's user avatar
3 votes
Accepted

Determining how much of a tile is intersected by a ray to help calculate lighting and deal with shadow issues

First of all a ray isn't going to help you with this. You will need at least 2 rays. "How much of a tile a ray intersects" is not really a meaningful question, since the ray will always split a tile ...
Nils Ole Timm's user avatar
3 votes
Accepted

How to avoid 2d enemy to see through walls

You only need to do two things: check if there is line of sight (raycast between the player and enemy) and if the angle of that ray is within the field of view (using atan2 to get an angle or using ...
David's user avatar
  • 86
3 votes

Alternatives to raycasts in unity?

There are other types of casts. Sounds like you might get some use out of Physics.BoxCast. Like the RayCast, BoxCast will return hit data that you can use to get all the same information as you would ...
House's user avatar
  • 73.2k
3 votes
Accepted

Debug.DrawLine doesn't go straight

Debug.DrawLine(Vector3 inWorldSpace, Vector3 inWorldSpace2, Color color) ...
Candid Moon _Max_'s user avatar
3 votes
Accepted

Pathfinding to Get In Attack Range with "Transparent" Tiles

"I know A* is good for finding a single destination and Dijkstra is better for multiple destinations" I would not categorize them this way. A* is just Dijkstra accelerated by a goal-seeking ...
DMGregory's user avatar
  • 133k
3 votes
Accepted

Understanding the rendering of the raycasting on flat screen

The trick here is to think of your image plane as though it was actually a flat object sitting in front of the camera, perpendicular to its view direction. This parallels the way the player's screen ...
DMGregory's user avatar
  • 133k
3 votes
Accepted

Raycasting from an object to the center of the screen (crosshair) without clipping through objects

You need to fire two rays: The first ray is fired from the player's-eye view, to find out what's under the crosshair, so you know what worldspace point to aim at. The closer that point is, the more ...
DMGregory's user avatar
  • 133k
3 votes

Intersection of thick line with a grid

If the thickness of your line / the radius of the circle following it is substantially narrower than your grid spacing, then it suffices to take the circle traversing your line and approximate it as a ...
DMGregory's user avatar
  • 133k
3 votes

How to snap a point to the height of the navmesh surface?

I found the function what does exactly what I need: ...
Robotex's user avatar
  • 251
3 votes

Line-of-sight algorithm for tile-based tactics

This is a partial answer. I can explain the cause of the problem you're seeing, but I don't have any proposal to fix it. Subsets of valid sightlights are not guaranteed to be valid sightlines This is, ...
Tim C's user avatar
  • 411
2 votes

How would I check the range against the entirety of an enemy object, and not just it's transform.position?

Unity provides this functionality out of the box with Collider.ClosestPointOnBounds and Rigidbody.ClosestPointOnBounds (for compound colliders). Testing whether an object is in range is as simple as:...
Ruther Rendommeleigh's user avatar

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