How to make ammo shell bounce

Godot Version

<stable4.2>

Question

<im trying to make ammo shell bounce. It sometimes successfully bounce, but sometimes it wont bounce. do you have any idea why? also, how can i make it bounce longer with small bounce force??

ezgif-7-930257dbe1

extends CharacterBody2D

var speed = 40
var gravity: float = 600.0
var bounce_damping := 0.9 
var bounce_damping_partial := 0.6

func _ready():
	set_as_top_level(true)
	velocity.y -= 300
	await get_tree().create_timer(5.0).timeout
	delete_object()

func _physics_process(delta):

	velocity.x *= 0.99
	velocity.y += gravity * delta

	if not is_on_floor():
		$AnimationPlayer.play("rotate")
	else:
		$AnimationPlayer.play("idle")

	move_and_slide()

	var collision = move_and_collide(velocity * delta)
	if collision:
		velocity = velocity.bounce(collision.get_normal())
		velocity *= bounce_damping
		#velocity *= bounce_damping_partial

The problem is that you’re using both move_and_slide() and move_and_collide(), so the object moves twice in a frame. If the object happens to hit the floor with move_and_slide(), it will slide along the floor.

The solution is to remove move_and_slide() from the code.

3 Likes