0 votes

Hi.

I want to detect collision using move_and_slide and get_slide_collision. That is to get the normal and know when the tile is a slope. This is my code:

extends KinematicBody2D

const Gravity = Vector2(0,500)
const Speed = 500
var linearVelocity = Vector2()

func _physics_process(delta):
    linearVelocity.y += Gravity.y * delta

    var left = Input.is_action_pressed("ui_left")
    var right = Input.is_action_pressed("ui_right")

    var horizontalForce = true

    if left:
        linearVelocity.x = -Speed
        horizontalForce = false

    if right:
        linearVelocity.x = Speed
        horizontalForce = false

    if horizontalForce:
        linearVelocity.x = 0

    var normal = Vector2(0,-1)
    linearVelocity = move_and_slide(linearVelocity,normal)

    var col = get_slide_collision(0)

All is fine but when I add the var col = get_slide_collision(0) the game turn slow.

What is the correct way to do this?

in Engine by (148 points)

2 Answers

0 votes

I notice that the game start turning slow when the character is not on floor. But do this:

if not is_on_floor():
    var col = get_slide_collision(0)

does not solve the problem.

by (148 points)
+1 vote

Well I found the solution:

var collisionCounter = get_slide_count() - 1
if collisionCounter > -1:
    var col = get_slide_collision(collisionCounter)
by (148 points)

Why are you adding the -1? Instead of just get_ slide _count() > 0?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.