Use get_slide_collision to get collider normal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gcivico

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?

:bust_in_silhouette: Reply From: gcivico

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.

:bust_in_silhouette: Reply From: gcivico

Well I found the solution:

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

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

CoverEye | 2019-01-09 11:56