Im making a game where you control a tank and the tank has an aiming laser that shows the path your shell will take when fired (straight line, just to make aiming far away easier as you see the path) but the RayCast2D is a single pixel wide or something and the tank shell is not, so it can show you can fire through a certain gap or against a wall or something when the shell will actually get caught on it.
Because of this, im changing the RayCast2D to a ShapeCast2D, by right clicking it in the tree and clicking “change type”. Now obviously i had to change some things like $RayCast2D.get_collision_point() to $RayCast2D.get_collision_point(0) but beyond that, no errors occurred and yet the aiming laser appears extremely jittery, just points directly up a lot of the time no matter where the turret looks, and overall acts weirdly. Cant find anything online so i came here to ask what else i may need to change to make a full conversion from a RayCast2D node into a ShapeCast2D node.
Provided is the code inside the “turret” node. It is before any changes were made.
extends Node2D
const MAX_RANGE = 1000000
var base_width = 8
var widthy = base_width
var shoot = true
var waiting_to_fire := false
var skip_frame = true
@onready var tank = get_parent()
func _ready():
for child in get_parent().get_children():
if child is CollisionObject2D:
$RayCast2D.add_exception(child)
$RayCast2D.add_exception(owner)
$RayCast2D.add_exception(get_parent())
$Line2D.clear_points()
$Line2D.add_point(Vector2.ZERO)
$Line2D.add_point(Vector2.ZERO)
func _process(delta):
$Line2D.width = widthy - 4
var forward = Vector2.UP
var max_cast_to = forward * MAX_RANGE
$RayCast2D.target_position = max_cast_to
if $RayCast2D.is_colliding():
var hit_pos = $RayCast2D.get_collision_point()
$Line2D.set_point_position(1, $Line2D.to_local(hit_pos))
skip_frame = not skip_frame
if skip_frame == false:
#some bot tank targeting code
else:
var end_point = global_position + max_cast_to
$Line2D.set_point_position(1, $Line2D.to_local(end_point))
$Line2D.set_point_position(0, Vector2.ZERO)
yes, a rectangle. And ive tried changing sizes, etc to no prevail. Also, (irrelevant) it may be an issue with it colliding with itself, however the option for “collide with parents” or something is disabled and the code:
for child in get_parent().get_children():
if child is CollisionObject2D:
$RayCast2D.add_exception(child)
$RayCast2D.add_exception(owner)
$RayCast2D.add_exception(get_parent())
$Line2D.clear_points()
$Line2D.add_point(Vector2.ZERO)
$Line2D.add_point(Vector2.ZERO)
in _ready() makes it so nothing of itself has collisions with it, so if its not colliding with itself, i have no clue whats causing it then
checking the checkbox for that debug makes the game crash once a tank (and its laser) are instantiated, no errors, just a “stopped responding” from windows. I did this however:
if $RayCast2D.is_colliding():
var currents = $RayCast2D.get_collider(0)
print(currents.name)
print(currents)
and it only ever returns “TileMapLayer”.
however i have found the cause, but not the solution;
i added to the else for the if statement mentioned above- a print(“no collisions”) before the code that sets the max line distance and whenever the line is not pointing where it should be (straight up) its printing “no collisions” every frame but when it is pointing where it should, it doesnt print that. Meaning it thinks that its not colliding with anything even when it is (as seen in the image, there is nowhere it could possibly point without colliding with something) but i dont know why this happens and why it never happend with the RayCast2D (also, when you change the node to a ShapeCast2D, the name stays the same to not break any $RayCast2Ds, thats why the code features $RayCast2Ds even though im using ShapeCasts now.)
I have found the solution; I removed a part of my code which was defaulting where the line should point if there are no collisions detected, but its still jittery since there are in betweens where it thinks it isnt colliding and remains in the last position it was. I fixed this by reducing the maximum distance it casts to which greatly improved collision detection and gave it a 0.04 margin. I then messed around with the thickness till it was matching the shell’s width and aiming laser’s width