#How do I make the velocity.y of my CharacterBody2D vector instead of a scalar?

72 messages · Page 1 of 1 (latest)

coral beacon
#

I want to be able to play a Falling and Rising animation whenever the velocity.y vector is either positive or negative, but its scalar by default.

#

(Im using godot's default CharacterBody2D script).

plucky olive
#

Velocity is a vector, it's components are not. But why can't you check if it's positive or negative?

coral beacon
#

not a vecor, but a scalar

#

its just the magnitude of the velocity, but not its directiion

terse shard
#

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

coral beacon
terse shard
#

is it falling?

coral beacon
#

yes

#

and this is right after a jump

#

ive just found this out

terse shard
#

then it supposed to be positive

coral beacon
#

didnt see it before

#

but it went negative for only a moment then positive

terse shard
#

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

coral beacon
#

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

terse shard
#

yes, then maybe the gravity is too strong

#

as it snap from -600 to 244 immediately

coral beacon
#

im using godot's default gravity

terse shard
#

try showing the whole code

coral beacon
#

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)```
plucky olive
#

Where are you checking velocity.y?

coral beacon
terse shard
#

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

coral beacon
#

those two together make the player unable to jump

terse shard
#

right remove the later one about apply_gravity()

coral beacon
#

still the same issue

#

the velocity.y is positive even when going up

plucky olive
#

I mean where are you choosing the animation?

coral beacon
plucky olive
#

It could be relevant

coral beacon
#

not exactly since the choosing between what animation to show is just a conditional statement checking if the velocity.y is positive or negative

plucky olive
#

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

coral beacon
#

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*

plucky olive
#

Is there anything else in this file you've left out?

coral beacon
#

nope

#

just the first line extends characterBody2D

terse shard
#

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

coral beacon
#

huh thats weird

terse shard
#

here where it transition from negative to positive, from jumping to falling, so there probably something else

coral beacon
#

hm

#

is it the collisionshape2d?

#

i have it as a capsule

terse shard
#

no i have it as capsule as well

coral beacon
#

the ground surface is a rectangle collider

#

i have literally

#

no other scripts on this project

terse shard
#

did you touch the project gravity setting

coral beacon
#

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

terse shard
#

characterbody can push rigidbody

#

so i guess they both fall together? idk

#

whatever its fine now, what a problem lol

coral beacon
#

ya

#

thanks!! @plucky olive @terse shard

#

I appreciate the help 😄