Changing object size based on player input

Godot Version

4.3

Question

Hello, I’m working on a fishing game. I want to move my rod based on the player’s input (either WASD or the left joystick)


Here is what the rod looks like, I didn’t finish the art sadly but the idea is there. The CollisionShape2D is at the end of the rod.

Essentially when a player moves the control up (pressing W or moving joystick up for example), the rod will shrink in size. Likewise, if they move it down, it will increase but no larger then its current size. Left and right and it will move it’s left and right, following this arrow:

How could this be achieved? I am lost honestly

You need to scale and rotate the Rod node. Put the script in the Rod (StaticBody2D) node. Something like this. Note I am using arrow keys, but you can change them in the project settings input map.:

extends StaticBody2D

# Define the scaling increment
var scaling_increment = 0.1

# Rotation
var rotation_speed = 3.0  # Adjust as needed
var rotation_direction = 0  # 1 for right, -1 for left

# Check if the key is being pressed in the _process function
func _process(delta):
	if Input.is_action_pressed("ui_up"):
		# Increase the scale
		if scale.y <= 1.0:
			scale = scale + Vector2(0, scaling_increment)
	elif Input.is_action_pressed("ui_down"):
		# Decrease the scale
		if scale.y >= 0.1:
			scale = scale - Vector2(0, scaling_increment)

#rotation code
	rotation_direction = 0
	if Input.is_action_pressed("ui_left"):
		rotation_direction -= 1
	if Input.is_action_pressed("ui_right"):
		rotation_direction += 1

	# Apply rotation if input is active
	if rotation_direction != 0:
		rotate(rotation_direction * rotation_speed * delta)


1 Like

Thanks for the help, it worked. I have another question:


I have a CollisionShape2D at the end of the hook that I use to detect if a fish touches it. How can I anchor it to the Hook Panel somehow so it forever sticks on at the edge like it does in the picture?

Create a fish scene. Instantiate it manually in the editor or using code as a child of the Hook/rod node. Then hide/unhide it, depending when you want to show it.

Of course you can also delete the node and instantiate it in code at will. Read this complete page on adding/deleting scenes via code here.

You can also hide a scene by setting it’s visible property…

E.g.
$Hook/Fish.visible=true/false

I’m afraid you misunderstood what I meant.


If I resize/rotate the hook, the Collision Shape does not follow and I was wondering if there was any way to anchor it so that it does. I already implemented detecting the fish.

Alright, I figured it out by adjusting the scale of CharacterBody2D rather than the Hook itself.

1 Like