Is there a way to make velocity 0 when you collide with something?

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

At the moment my character will slide around an object whenever I run into one(I don’t mean it’ll just go around it, I mean whenever I run into something it slides awkwardly. If you still don’t understand here’s a video file: It’s very disorienting and weird. I’ll put the code below this so if there is a way to add this I would greatly appreciate it.

const ACCELERATION = 500
const MAX_SPEED = 80
const FRICTION = 500

func move_state(delta):
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")"Walk")
	input_vector = input_vector.normalized()
	if input_vector != Vector2.ZERO:
		animationTree.set("parameters/Idle/blend_position", input_vector)
		animationTree.set("parameters/Walk/blend_position", input_vector)
		animationTree.set("parameters/Attack/blend_position", input_vector)
		velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
	velocity = move_and_slide(velocity)
:bust_in_silhouette: Reply From: subtree

I think this might work.

var collision
collision = move_and_collide(velocity*delta)
if collision != null:
    velocity = Vector2(0,0)

But it uses move_and_collide() instead of move_and_slide(). I think you can implemet using move_and_slide() too.

You will have to replace:
velocity = move_and_slide(velocity) with the above code

subtree | 2020-12-07 04:21

no, if I use this I can’t move at all :confused:

PizzaOnFire99125 | 2020-12-07 15:13