Warp function done, but

Godot Version

Godot Engine V4.3

Question

Here is my current code:

var MAXENERGY = 400
var energy: int

var cursor: Vector2
var cost: int

func _process(_delta: float) -> void:
	var distance = ((position.x - cursor.x) ** 2 + (position.y - cursor.y) ** 2) ** 0.5
	
	get_global_mouse_position()
	cursor = get_global_mouse_position()
	
	cost = 0.2 * distance
	
	if energy < MAXENERGY:
		energy += 1
	
	if Input.is_action_just_pressed("LClick"):
		if energy >= cost:
			verticalspeed = 0
			currentspeed = 0
			position = Vector2(cursor)
			energy -= cost
			print(cost)
		else:
			print("Not enough energy") `

So basically this allows the character to teleport to the cursor on click when it has enough energy (it regens over time), however now the problem is that it can clip through thin walls (currently using StaticBody2D and CollisionBody2D as simple ones). Any tips on how can edit this so it does not go through walls anymore?

Regards, Godot newbie

It looks like your code teleports directly to the cursor, with enough energy couldn’t the player clip through large walls too?

You could use a RayCast to ensure the player does not move through walls, I would recomment intersect_ray over the RayCast2D node as documented here

1 Like

Unrelated to your clipping problem, I noticed you’ve tied your energy regen to the frame rate, the higher the frame rate the faster you’ll gain energy. You should multiply the energy gain by the delta passed into _process

	if energy < MAXENERGY:
		energy += 1 * _delta

This would make everyone gain 1 energy per second regardless of framerate. You can adjust the constant (1 in this case) to gain more or less energy per second.

Sorry if you already knew this and were just prototyping/trying stuff out. Thought I’d mention as you signed off as a Godot newbie :smiley:

2 Likes