First 3d prototype attemp

Godot Version

Replace this line with your Godot version4.5.dev1

Question

Hi, after having looked around to get information as much as possible, I. started to implement a 3d protorype; more in. detail I did a scene named ‘player’ which can just perform basic actions like: walking and jumping. First of all, my concern is about handling Jump action. followin there is Implementation I tryed but I seems it doesn’t work at all…player doedn’t JUMP!!!
The code is:

extends CharacterBody3D

const SPEED = 45.0
const JUMP_VELOCITY = 45.5


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		print('y after jumping: ',velocity.y)

Is that the entire script? If so you must call move_and_slide() for the character to move, and is_on_floor() to evaluate.

sorry, I just forgot to paste all the code; the right version is:

extends CharacterBody3D


const SPEED = 45.0
const JUMP_VELOCITY = 45.5


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		print('y after jumping: ',velocity.y)

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	## var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	var input_dir := Input.get_vector("left","Right","Up","down")
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	#print('direction : ',direction)
	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)

	move_and_slide()

Do you recieve any errors? Your using a built-in action for jumping “ui_accept” this only maps to the keyboard’s spacebar.

1 Like

no, not any error message get printed and the game print resulting jump action pressed when I press Enter button on the keyboard…

You mean this print is showing in the console, right?

print('y after jumping: ',velocity.y)

I attach a screenshot of what I see:


But tha character doesn’t move

I am stuck yet within my first 3d game attempt:
I have as root node of the scene a Node3D and one of its children is scene which is CharacterBody3D type: the Player:


Attached to the Player there is the following script:


extends CharacterBody3D

const GRAVITY = 5
const SPEED = 45.0
const JUMP_VELOCITY = 20.0

func _ready():
	print('ALL IS READY')
	print('velocity vector is: ', velocity)

func _physics_process(delta): # _physics_process 
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("Jump") and is_on_floor():
		velocity.y += JUMP_VELOCITY
		print('y after jumping: ',velocity.y)
			
	move_and_slide()

It is very easy approach just to start…but the problem is when I run the game and press the Input command linked to “Jump” (I defined it in the Input Map)…I cannot see any character movement, :confused:

I think JUMP_VELOCITY should be declared negative, anyway I tryed It but nothing change…

What’s the size of one of your CSGBoxes? Is your Camera3D a child of the player? Is the camera set to Top Level by accident?

This is a screenshot such that it is possible looking all the CSGBoxes (3 cubes):

Player scene is so formed:

Have you tried setting a jump velocity to something like 10000?
If the scales are messed up, maybe a force of 45 is actually very low compared to the size of the character, which would mean that the code is working, but you just don’t see anything as there’s no jump at all.

i tryed to set const JUMP_VELOCITY = 10000.0 but nothing changed…I am going to update the script —>

extends CharacterBody3D

const GRAVITY = 2
const SPEED = 45.0
#const JUMP_VELOCITY = 10000.0 #60.0
var target_velocity = Vector3.ZERO
@export var jump_strongness = 40000.0

func _ready():
	print('ALL IS READY')
	#print('velocity vector is: ', velocity)
		
func _physics_process(delta): # _physics_process 
	
	velocity.y -= GRAVITY * delta
	if is_on_floor():
		print('--- Is On floor __>')
	else:
		print('on Air  !')
		
	if Input.is_action_just_pressed("Jump") and is_on_floor():
		target_velocity.y = jump_strongness
		print('I have Jumped !!')
		
	move_and_slide()