i followed every tutorials i found on the web but i sti have the same issuer over and over: collision is not working.
I create a character body 2d, then a sprite, then a collision shape. I create a static body then a collision shape to but the player he’s still passing through the area.
Is there something a miss there?
I’ll really appreciate some help with my begining in Godot.
You are modify the position of the CharacterBody2D. This bypasses the physics engine, which handles collisions. Instead you should use move_and_collide() or move_and_slide() methods.
Your code generate with BlockCode:
extends CharacterBody2D
func _ready():
pass
func _process(delta):
if (Input.is_action_pressed('ui_right')):
position += Vector2(5, 0)
if (Input.is_action_pressed('ui_left')):
position += Vector2(-5, 0)
It can be like this:
extends CharacterBody2D
var speed: float = 200.0
func _process(delta):
var motion: Vector2 = Vector2.ZERO
if Input.is_action_pressed('ui_right'):
motion.x += 1
if Input.is_action_pressed('ui_left'):
motion.x -= 1
motion = motion.normalized()
velocity = motion * speed
move_and_slide()