How to work around move_and_collide

Godot Version

4.2.2

Question

extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):
	if not is_on_floor():
		velocity.y += gravity * delta

	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	move_and_slide()

	var collision = move_and_collide(velocity)
	var collider
	if collision:
		collider = collision.get_collider_velocity()
		velocity = 10*collider

i need move_and_collide because i have another character 2d node that needs to be able to shove the main [the code above] character2d node

but whenever i add move_and_collide into my code it makes the play move too fast but also really slow on slopes

another way to detect player collision or to stop move_and_collide from doing that would be great
(i mean yeah i could use area2d and make it slightly larger then the player but then i’d need to code in what happens when the player enters from what angle)

You shouldn’t be calling both move_and_slide and move_and_collide simultaneously. move_and_slide should be able to do what you want well. You can detect collisions with get_slide_collision etc. and change the other body’s velocity.

1 Like

so i got rid of the move_and_collide and now have

var collision = get_slide_collision(0)
if move_and_slide():
	print(collision.get_collider())

and it works on start up outputting
(173) TileMap:<TileMap#27816625351>
or
(146) clone:<CharacterBody2D#27934065908>
but when i touch something else/leave the ground and touch anything)

it
image

if collision:
      print(collision.get_collider())
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.