Collision not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mister_why
:warning: Old Version Published before Godot 3 was released.

Hello !

I’m making a game (no kidding), and the object (kinematicsbody2D) isn’t colliding with the wall (Staticbody2D).

The objects has a collision shape, and it still a mystery to me why it doesn’t work.

A beer to the person who can resolved my problem ^^

extends KinematicBody2D

var SPEED = 1000
var MAX_SPEED = 3000
var vel = Vector2()
var JUMP_FORCE = 1500

func _ready():
#set_process(true)
set_fixed_process(true)

func _fixed_process(delta):
if Input.is_key_pressed(KEY_SPACE):
vel.y = -JUMP_FORCE
elif Input.is_key_pressed(KEY_DOWN):
vel.y = SPEED

elif Input.is_key_pressed(KEY_RIGHT):
	vel.x = SPEED
	
elif Input.is_key_pressed(KEY_LEFT):
	vel.x = -JUMP_FORCE
	
else:
	vel = Vector2(0, 0)
#set_pos(get_pos() + vel * delta+ Vector2(0,9.81))
set_pos(get_pos() + vel * delta)

Not enough info, you need to upload your project or at least post all the relevant scripts and your scene hierarchy.

raymoo | 2017-07-14 02:08

as the one above said, not enough information but by guessing, check that all the collision shape’s trigger option is set to false(not ticked) and that you are using move() and not set_pos() in order to move your character

rustyStriker | 2017-07-14 10:10

I’ve checked the code you posted and I don’t see anything related to collision. But there are some typos in it like, you used setfixedprocess() instead of set_fixed_process() and the function fixedprocess() instead of _fixed_process(). These are functions in Node class.

codevanya | 2017-07-14 16:43

Nope the set_fixed_process() etc are good, it’s the copy and paste who eat all of the “_” ^^

But, the collisions, are defined with the collision shape isn’t ? If I put 2 KinematicBody2D with sprites and collision shape, they collided isn’t ?

mister_why | 2017-07-14 17:21

@codevanya You just can’t see the underscores because the Q&A-site interprets them as markdown because the OP didn’t wrap his code in a code block (intend with four spaces).

timoschwarzer | 2017-07-14 20:11

:bust_in_silhouette: Reply From: raymoo

The reason it doesn’t work is because set_pos does not check for collisions. For KinematicBody(2D)s, you need to use the move method instead of using set_pos, if you want collisions. move takes a displacement argument, like this:

move(vel * delta)

So just replace your set_pos call with that.

It works !!

So set_pos is for objects who doesn’t have collision shape ?

mister_why | 2017-07-14 17:55

set_pos is just for setting the position, doing no checks or anything. You can use it with collidable things if you want, it just won’t do collision checking. For example, if you’re teleporting the character then you don’t want to collide with walls that are in the way.

raymoo | 2017-07-14 17:58