#How do I make the velocity.y of my CharacterBody2D vector instead of a scalar?
72 messages · Page 1 of 1 (latest)
Velocity is a vector, it's components are not. But why can't you check if it's positive or negative?
velocity.y is actually just speed
not a vecor, but a scalar
its just the magnitude of the velocity, but not its directiion
how do you get this conclusion? velocity is a vector and velocity.y is one of its two component which can be positive or negative
All the values its outputing are positive for me
is it falling?
then it supposed to be positive
in godot and most program doward direction is y positive and upward is y negative
velocity.y went negative for a moment probably cause for a jump, then the gravity act upon it, pulling it down to falling and causing velocity.y to be positive
based on how i look at the default characterbody2d code
but its motion is upward until gravity's affect on the velocity overtakes the jump right?
so theoretically it should be negative until the character starts descending
im using godot's default gravity
try showing the whole code
const SPEED = 300.0
const FRICTION = 1.5
const JUMP_VELOCITY = -600.0
const DASH_VEL = 200
var jumps_left = 2
@onready var animated_sprite = $AnimatedSprite2D
var pre_jump_finished = false
var pre_jump = false
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _process(_delta):
animation_handler()
func _physics_process(delta):
apply_gravity(delta)
input_handling(delta)
print(velocity.y)
func apply_gravity(delta):
# Add the gravity if not on floor.
if not is_on_floor() and !Input.is_action_just_released("ui_up"):
velocity.y += (gravity*0.5) * delta
elif is_on_floor():
jumps_left = 2
move_and_slide()
func input_handling(delta):
## JUMP ##
if Input.is_action_just_released("ui_up") and jumps_left>0:
velocity.y += JUMP_VELOCITY
jumps_left -=1
## Horizontal Movement ##
# Get the input direction and handle the movement/deceleration.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else: ## STOPPING (FRICTION)
velocity.x = move_toward(velocity.x, 0, SPEED*0.1)```
Where are you checking velocity.y?
as in the print? its in the physics_process function
on the jump code, instead of velocity.y += JUMP_VELOCITY, do velocity.y = JUMP_VELOCITY
and on apply_gravity(), when is_on_floor(), set velocity.y = 0
those two together make the player unable to jump
right remove the later one about apply_gravity()
I mean where are you choosing the animation?
further down in the code, Im not showing it just to not waste your times with that
It could be relevant
not exactly since the choosing between what animation to show is just a conditional statement checking if the velocity.y is positive or negative
I don't see why this code you posted would be causing this. I have ot assume you have code elsewhere that is causing a problem
the value of any variables are not changed
func animation_handler():
# Y axis movement animations
if Input.is_action_pressed("ui_up") and is_on_floor() and velocity.x == 0: # WIND UP
animated_sprite.play("pre-jump")
if velocity.y >0 and !is_on_floor():
animated_sprite.play("Falling")
# if pre_jump == true:
# animated_sprite.play("Wind-up")
# X axis movement animations.
if velocity.x != 0 and velocity.y == 0:
animated_sprite.play("Run")
elif velocity.x == 0 and velocity.y ==0 and !Input.is_action_pressed("ui_up"):
animated_sprite.play("Idle")
if velocity.x >0:
animated_sprite.flip_h = false
if velocity.x <0:
animated_sprite.flip_h = true```
there is he rest of it
the*
Is there anything else in this file you've left out?
i copypasted your code and tested it, i got how it expected to be like, 0 to jump_speed, then slowly moving toward positive as its falling
huh thats weird
here where it transition from negative to positive, from jumping to falling, so there probably something else
no i have it as capsule as well
the ground surface is a rectangle collider
i have literally
no other scripts on this project
is it because i used a rigidbody instead of a staticbody for the ground?
yup
changed that
and now its fixed
thanks
i saw you used a staticbody node