I have a wall-crawling enemy that for some reason always pushes away from the wall in fluctuating amounts. I have adjusted the hitbox, changed code and nothing has worked. The code is below. Can anyone tell me what might be causing it?
extends CharacterBody2D
var SPEED = 20 @export var direction_unneeded : Vector2 @export var movedir : float
var direction : Vector2
var waittime := float(1)
Seems like the movedir is influenced by the direction twice in these places, just odd not sure if it’s relevant.
If this line is ment to rotate towards a wall then it shouldn’t be multiplied by -movedir. You may be able to get a more consistent result using get_collision_normal() if “Side” is a raycast.
With a CharacterBody2D you will want to edit their up_direction to detect “walls” or “floors”, you’ve probably worked around this by using a raycast named $Down.
You can replace direction with transform.x in 2D to get a “forward” vector based on the current rotation.
But all of these recommendations are minor changes, I’m not sure exactly what’s going wrong. Could you explain what you expect to happen vs what really happens? maybe a screenshot or two?
To format pastes place your code between three ticks like so
I don’t know why the enemy hovers over the platform after it turns. The code should only change rotation, and then adjust around corners, but I don’t see why it offset the position.
And the movedir just designates if it goes left or right, which is why I change it around, because it needs to adjust to each angle.
Looks really good! I suspect it’s turning too quickly and the collision shape pushes it away slightly, could you use the raycast to set the position closer to the wall?
i.e. global_position = $Down.get_collision_point()