Godot Version
4.3
The issue
I’m working on a 2-player game, it’s essentially ice hockey but in 2D. The hockey stick and the hockey puck are both Rigidbody2D nodes. The hockey stick’s global position is constantly set to the player’s to prevent it from falling down. The stick changes direction by changing the angular_velocity property.
Most of the times the stick succesfully shoots the ball, however certain times the puck doesn’t move well . It also glitches inside walls.
Link to the video showing the bugs
When I observed the video frame by frame, I noticed that the hockey stick pushes the puck down instead of up, that causes the ball to not be shot properly. However I don’t know how to solve this issue neither.
Ball rigidbody properties:
Hockey stick rigidbody properties:
Part of the hockey stick script that applies forces
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
var dirs = [[-90, -40], [-45, 30]]
if hstate != oldstate:
tick = 0
global_position = get_parent().global_position
if hstate == hockeyState.prepare:
state.angular_velocity = 0
rotation = deg_to_rad(dirs[player-1][0])
state.angular_velocity = -30 * sens
hstate = hockeyState.ready
elif hstate == hockeyState.shoot:
if tick == 0:
state.angular_velocity = 50 * sens
elif rad_to_deg(rotation) > dirs[player-1][1] and tick > 0.05:
hstate = hockeyState.retake
elif hstate == hockeyState.retake:
if tick > 0.55:
if state.angular_velocity > -50:
state.angular_velocity = -10 * sens
if rad_to_deg(rotation) <= dirs[player-1][0]:
hstate = hockeyState.idle
state.angular_velocity = 0
rotation = deg_to_rad(dirs[player-1][0])