-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
changes to default characterbody scripts #66723
Conversation
modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
Outdated
Show resolved
Hide resolved
bafc150
to
2a9359f
Compare
modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd
Show resolved
Hide resolved
modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd
Outdated
Show resolved
Hide resolved
modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd
Outdated
Show resolved
Hide resolved
modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
Outdated
Show resolved
Hide resolved
2a9359f
to
aaa8345
Compare
I currently have my PC under repair so I can't currently do tests :( I would avoid to change the logic of gravity as long as it doesn't fix any problems, because there is a good chance that this change will introduce jitters or other issues. For example, you will never move only to the right/left, but always diagonally, I am not sure that in some corners/slopes this does not cause small problems. The current template has been pretty well tested and since this is a character controller, not a rigid body simulation, it does not have to strictly obey the physical rules. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to what I wrote about the gravity change, there are the same templates for c#, they must be synchronized: https://github.com/godotengine/godot/tree/master/modules/mono/editor/script_templates
If anything this is changing it back to how it's always worked. In 3.x not applying constant gravity is the number one source of |
I'd like to revisit this. As we have a lot more new users coming in, it's really important that the default script they'll see be a simple and straightforward one. Are we waiting for the C# changes to commit? I can make those, but I'd need someone to review, because I'm not running the .net version to test it on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still LGTM. Still needs C# changes. Not critical for 4.0 though.
Well, there are concerns from physics maintainers above, and no resolution on that front after that. Would be nice to reach some consensus before making the change. (Having parity between languages should be important too). |
@cbscribe Sorry, I had completely forgotten about this PR!
I don't quite understand the changes because this PR removes the deceleration (so the two bottom points are false).
You are right. IMO two things can be done to improve the script:
(For the second point, opinions are welcome.) |
@fabriceci Thanks for the clarification. If it's the case that it works better, then I guess that's the way we'll do it now. It just feels wrong to be effectively turning gravity on and off all the time. I'll revise to just deal with the acceleration/deceleration code. |
@cbscribe @aaronfranke here is my suggestion, I tried to keep it very simple like it was but I added some options to make it more flexible, more ready to be used. It is necessary to tweak a bit the default values, maybe the extends CharacterBody2D
@export var WALK_MAX_SPEED := 500.0
@export var WALK_ACCELERATION := 1500.0
@export var RUN_MAX_SPEED := WALK_MAX_SPEED * 1.3
@export var RUN_ACCELERATION := WALK_ACCELERATION * 1.3
@export var FRICTION := 1000.0
@export var SLIDE_FRICTION := 1000.0
@export var JUMP_VELOCITY := -400.0
# Get the global gravity from the project settings. RigidBody2D nodes also use this value.
var gravity: float = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta: float) -> void:
# The run action is not defined by default in the project, you must add it yourself.
var running = Input.is_action_pressed("run")
var acceleration := RUN_ACCELERATION if running else WALK_ACCELERATION
var max_speed := RUN_MAX_SPEED if running else WALK_MAX_SPEED
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Allow jumping only when on the floor.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Apply friction.
velocity.x = move_toward(velocity.x, 0, FRICTION * delta)
# Get the input direction and handle the movement.
# As good practice, you should replace UI actions with custom input actions.
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
# If the direction is opposite to the x-velocity, extra friction is applied for more reactivity.
if direction + sign(velocity.x) == 0:
velocity.x += SLIDE_FRICTION * direction * delta
velocity.x = clampf(velocity.x + acceleration * direction * delta, -max_speed, max_speed)
move_and_slide() |
Superseded by #73873 |
Updates to default scripts for CharacterBody2D/CharacterBody3D.
SPEED
asmove_toward()
factor just means stopping instantlyFRICTION
consistent with other values - result: stops in ~0.5 sdelta
when deceleratingRemove static typing hintsDefault in godot-docs is no type hints"Type Hints" defaults off in Editor Settings