How could I change move_and_slide to move_and_collide?

UnrulyElf10UnrulyElf10 Posts: 37Member
edited July 26 in General Support

Hi, I code on GDScript and I have recently followed along with a tutorial on how to make a 2D platformer game. The guy making the tutorial added in a thing called move_and_slide, however I encountered some problems with then navigating through my level, ad the player would slide all over the place. I then tried to swap out move_and_slide with move_and_collide, and it said this: res://Player.gd:53 - Parse Error: At "move_and_collide()" call, argument 2. The passed argument's type (Vector2) doesn't match the function's expected argument type (bool).

I will copy and paste my code for anyone interested in trying to help me fix this- any help would be appreciated!

-----Start of code------

extends KinematicBody2D

const up = Vector2(0, -1)
const gravity = 50
const max_fall_speed = 1000
const max_move_speed = 300
const jump_force = 1250
const acceleration = 5

var motion = Vector2()
var facing_right = true


func _ready():
    pass

func _physics_process(delta):

    motion.y += gravity
    if motion.y > max_fall_speed:
        motion.y = max_fall_speed

    motion.x = clamp(motion.x,-max_move_speed,max_move_speed)
    if facing_right == true:
        $Sprite.scale.x = 1
    else:
        $Sprite.scale.x = -1

    if Input.is_action_pressed("left"):
        motion.x -= acceleration
        facing_right = false
        $AnimationPlayer.play("Run")
    elif Input.is_action_pressed("right"):
        motion.x += acceleration
        facing_right = true
        $AnimationPlayer.play("Run")
    else:
        motion.x = lerp(motion.x,0,0.05)
        $AnimationPlayer.play("idle")
    if is_on_floor():   
        if Input.is_action_pressed("jump"):
            motion.y = -jump_force
            $AnimationPlayer.play("Jump")

    if !is_on_floor():
        if motion.y < 0:
            $AnimationPlayer.play("Jump")
        elif motion.y > 0:
            $AnimationPlayer.play("Fall")
        elif motion.y == 0:
            $AnimationPlayer.play("idle")

    motion = move_and_slide(motion, up)

-----End of code------
If you can see above, it says move_and_slide, and I'm trying to change that so that I don't slide about, instead I just move and collide with objects.~~~~


Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,810Admin

    Well, move_and_collide is not a drop-in replacement for move_and_slide, if nothing else other than move_and_slide returns a Vector2 that is the remaining velocity, while move_and_collide returns collision info. Additionally, move_and_collide only takes a single argument (documentation), which is just the motion for the KinematicBody2D.

    To replace it, you'll need to change line 53 to the following:

    var collision_info = move_and_collider(motion)
    

    And then it should work. That said, motion will not stop when colliding into objects or anything with the code above, it will just move the KinematicBody2D through space and collide into objects.

  • UnrulyElf10UnrulyElf10 Posts: 37Member

    ah- well then I may try something else or attempt to stop the character sliding around so much

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file