How do you bounce a CharacterBody2D in Godot 4?

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

Please somebody point me in the right direction.
(I’m trying to find a solution for the past 6 hours)
After a bunch of tutorials and forum posts (mostly from Godot 3, so not that helpful)
I thought my best bet would be the documentation Physics Introduction, however the example there too seems to be off.
Here’s my code:

extends CharacterBody2D

var my_velocity = Vector2(250, 250)

func _physics_process(delta):
var collision_info = move_and_collide(my_velocity * delta)
if collision_info:
	my_velocity = my_velocity.bounce(collision_info.normal)

It starts, but on a collision this gets me the error:
Invalid get index ‘normal’ (on base: ‘KinematicCollision2d’).

If you look in the example from Physics Introduction, I made some changes because extends PhysicsBody2D gives error Could not find base class "PhysicsBody2D
Can I extend PhysicsBody2D somehow?

and I had to change velocity too because it’s a member of CharacterBody2D and can’t be redefined.

I’m trying to make a Pong type of game, I’m a beginner too … soo … Do you think I would be better off with learning Godot 3 instead? I can’t really wrap my mind around the changes and lack of tutorials for Godot 4, even the documentation is confusing. :frowning:

If you look at Using CharacterBody2D and specifically move_and_slide there is a bunch of stuff there like infinite_inertia from Godot 3, it has nothing to do with Godot 4 anymore (as I learned the hard way).

:bust_in_silhouette: Reply From: Wakatta

Question: Why would you use an undocumented version which probably hangs, freezes and is likely to corrupt your projects.

Hopefully the answer is To test and not for actual game production.

From observations of the source Godot4 seems very newb friendly pre-defining vars and even going as far as to offer template scripts and punish bad programming behavior such as directly accessing variables.

Listen well Padawan you would do well to not learn programs but the concepts of those programs so your skills can easily be transferred.

Your Answer

func _ready():
	set_velocity(Vector2(250, 250))

func _physics_process(delta):
	var collision_info = move_and_collide(velocity * delta)
	if collision_info:
		velocity = velocity.bounce(collision_info.get_normal())

Thank you for the answer, and your time!
Humble Answer: As all of our time is limited, I thought maybe I should try learning Godot 4, with the mindset of why should I start learning Godot 3 now, if a new modified (and pretty different) version is already on the way (but I get it now, I will stick to learning Godot 3).

Since gaming is a passion of almost 30 years for me and I always admired how games were made. As a mechanical engineer so not even in a related field, it’s just a hobby that gives me joy to dedicate a couple of hours daily to pursuit, not trying to make anything fancy, just learn and make something.

I’m aiming for the concepts, that’s why I looked up many tutorials and examples, to grasp it, but the lack off success made me to post my sour question (sorry!)

Thanks again for the answer and for pushing me in the right direction!

Byo | 2022-11-05 15:58

it’s just a hobby that gives me joy to dedicate a couple of hours
daily to pursuit

Such a joy that brings a tear to mine eye.

Your goal is actually a smart one and yes you should practice Godot4 now so you will have a great understanding and grasp for when it gets stable.

To reiterate have seen companies use specialized software for their purposes then when they switched to commercial ones like AutoCad, MSExcel, QuickBooks most of the employees couldn’t keep up because they became grounded in the software and not the concepts.

And even see it here sometimes “Used to do this great awesome thing in Unity / CryEngine / Unreal Engine how to do it in Godot?”

The right tool to drive nails may be a hammer and there are certainly better ones like a nailgun but the skill of driving nails can make you achieve it even with a rock.

Wakatta | 2022-11-06 09:43

My humble opinion is learn Godot 4 now. There are way less doc on it but you seem smart and Godot 4 is the futur.
Beside lots of Doc of Godot 3 apply on 4 or can be adapt with not much trouble except for a few exceptions i guess.

StillWaiting | 2022-11-09 02:16

Thanks for the feedback guys, both of you make a very good point!
At least for now, I’m getting more familiar with Godot 3.5.1. Even though the majority of code is applicable to Godot 4, there are some differences and I had some rough days (because my lack of understanding the concepts), and since the past couple of days I’m having a blast with Godot 3 by making stuff that actually works.
Still much to learn, but searching through the web and seeing a mostly healthy community for such a great game engine … it’s one of those happy to be alive moments!

Byo | 2022-11-10 18:08

From the future projections for Godot 4 looks like there will be a backward compatibility assistant to import Godot 3.x projects. So learning version 3.x first is definitely a plus.

Speaking of rocks, you won’t find a harder one than Pascal or Assembly
And yet Chris Sawyer was able to code one of the best selling games of its time (RollerCoaster Tycoon) almost completely with it.

Nowadays we have full physics and real-time rendering engines, archives of free information and dedicated communities willing to help. And everyone is busy creating MineCraft and Flappy bird clones.

Looks like you really can only find diamonds in the rough, and for you as a mechanical engineer, should know that’s starting with a good foundation first.

Wakatta | 2022-11-11 22:56

Dude i create this account just to thank you, i am doing the same project, and this damn “get_normal()” was the root of my problems

Cowk | 2023-03-19 19:42