Ricocheting bullet

I’m trying to make a Ricocheting Bullet on my project
I tried guessing the logic for inverting the direction by looking at a functional enemy that would invert it’s movemment direction once it touched a wall, but it didn’t work and the bullet just stops on the wall
Can somebody explain how can I properly do it?

func _physics_process(delta):
translate((speed*dir)*delta)
var collision_info = move_and_collide(velocity * delta)
if is_on_wall():
	speed.x *= -1

Have you tried printing the speed of your bullet when it touches a wall?

It alwasy apperas to be a the regular value of 200

Would you mind showing your entire script for your bullet? It’s hard to debug without the full context.

Here:
extends CharacterBody2D

var speed = 200

var dir : Vector2
func _physics_process(delta):
translate((speed*dir)delta)
var collision_info = move_and_collide((speed
dir)*delta)
if is_on_wall():
speed.x *= -1
print(speed)

func _on_visible_on_screen_enabler_2d_screen_exited():
queue_free()

Okay…

You’re clearly not understanding your own code. Here’s a line for line walkthrough of how I interpret your code:

// Your class is inheriting the functionality of this node type
extends CharacterBody2D
// A variable that describes how fast your bullet should move
var speed = 200
// Creating a Vector2 variable for controlling the direction?
// You are not assigning any value to this variable.
// Are you setting its value outside this script? If so, it's not the best practice.
var dir: Vector2
// Here you move the position of the bullet based on your speed and direction
translate((speed * dir) * delta)
// Once again you move the bullet but now through the move_and_collide method?
// You should not be doing translate AND move_and_collide.
var collision_info = move_and_collide((speed * dir) * delta)
// Now you're inverting the x-axis of a variable that is not a vector.
// Do you know what a datatype is or what a vector is?
if is_on_wall():
    speed.x *= -1
// I guess you're removing the bullet if it does not exists on the screen.
// This is fine as long as you do not have a moving camera.
func _on_visible_on_screen_enabler_2d_screen_exited():
    queue_free()

Your problem

Vectors are an important concept to understand when it comes to game development. Your script shows that you are not aware of what you are doing.

Your speed variable is one number - a number that is used to modulate how fast your bullet should be moving. You can’t expect to modify its X-value because it doesn’t have one.

The direction in which your bullet is moving is computed based on your dir variable - short for direction. However, this is not the variable that the bullet (which is a CharacterBody2D) is using to store the state of the bullet. CharacterBody2D has its own variable called velocity. This is the variable you should actually be modifying to control the direction (and speed).

Example:

velocity = speed * dir
var collision = move_and_collide(velocity * delta)

What you should do

I would recommend that you take a good look at the Godot Docs. Specifically, this section which describes and shows a simple case of how to implement a bullet with bouncing functionality, among other things.

With all of this said, I do commend you for trying to tackle the problem without help. Just remember that it’s very hard to solve a problem without knowing the basics.

2 Likes

Alright, thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.