Godot 4
Im trying to apply velocity in the direction the player is facing weather that be up, down, left, or right. additional info will be provided if needed
tried this
Is this related to your other posts? Your request seems in direct contradiction to this one, where you ask for movement to only affect the z axis, and seems like youāve almost succeeded.
Godot 4
I am trying to find a way to make this work on only the z axis and not affect the rest of the velocity
velocity = global_transform.basis.z * -SPEED
currently its affecting all velocity and I only want it to affect the Z axis, any questions can likely be answered
If itās not then you can remove .z
to apply the speed in any direction forward-facing and fix the type issue. It may need to be negative since forward is negative z.
velocity = -camera.basis.z * SPEED
but I think weāve been here before too
The unary negation operator - is always applied left-most -camera.basis.z.
Your camera may have a different transform than the characterbody, if you only rotate the camera then yes use camera.basis, but if you use a parent rotation as part of the camera you might need to use the global transform instead through camera.global_basis. For example most first-person controllers rotate the characterbody around the y axis, and rotate the child camera around the x axis. However most first person controā¦
Since Iām not sure what exactly you have set up or what you want, hereās a good template hopefully merged soon for a 3D, grounded player controller. It can jump, move in any direction it is rotated and seperates planar x/z movement from the gravity and jump based y axis.
If you could explain more about your scene tree and what your goals are for this project that would help us help you.
# meta-description: Classic movement for gravity games (FPS, TPS, ...)
extends _BASE_
# The @export annotation allows a variable to be shown and modified from the inspector.
@export var speed: float = 5.0
@export var accel: float = 5.0
@export var jump_speed: float = 4.5
func _physics_process(delta: float) -> void:
# Handle gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Get the vertical velocity.
var vertical_velocity: Vector3 = velocity.project(up_direction)
# Get the horizontal velocity.
var horizontal_velocity: Vector3 = velocity - vertical_velocity
This file has been truncated. show original
for your info it kinda is, just not the same part
1 Like
also thats about all i need to know about this stuff so thanks a lot for helping me learn this
1 Like
No problem, sorry for my confusion.
Hereās the git pull request for the template, the conversation brought up includes a lot of good information.
godotengine:master
ā yosoyfreeman:character_body_templates_fix
opened 07:19PM - 20 Dec 24 UTC
This PR improves the default CharacterBody template for both 2D and 3D. It fixeā¦ s the following issues:
**Input distortion:**
The line:
`var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()`
Performs the basis multiplication against a potentially non orthonormal basis. This is often the result of an object which doesn't have an scale of 1. However, because the normalization happens after the possibly wrong basis multiplication, the direction of the original input is now lost. This can be observed if you scale one of the axis to be bigger and move diagonally.
This has been fixed by removing the normalization (Which is not necessary because get_vector always reports at max a unit vector) and using an orthonormal basis instead. The scale no longer alters the input direction. This also allowed to use an smaller deadzone for the joystick, allowing the template to work with controllers too and illustrate how is done.
**Acceleration had multiple issues:**
It was accelerating instantly, but slowing down is done by moving the speed towards zero in an FPS dependent way, as there is no use of delta involved. Also, accelerating per axis is geometrically incorrect (Same issue as with non normalized vectors) and causes a very unnatural feeling. Also, the fps deceleration was the max speed.
```
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
```
Now we isolate the horizontal component of our velocity and perform correct acceleration/deceleration
```
# Move the current horizontal velocity towards the target horizontal velocity by the acceleration factor multiplied by delta.
horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, ACCEL * delta)
```
I have also taken into account the up vector of the CharacterBody node. We were ignoring it and assuming always an up vector of ` 0 1 0`. Now everything is done having into account that the up vector is arbitrary.
I have tested it and works nicely. The c# versions are my first attempt with MONO, so i would appreciate some help in those to make sure i followed conventions properly.