• 3D
  • Make jumping look better

I am making a game, where you switch between 2d and 3d. I can't get the jumping to look smooth:

As you can see, it just teleports, I want it to be smooth.

Here is the source code, tried to take some code from the platformer tutorial by HeartBeast and also read some of the comments to understand things:

extends KinematicBody


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
onready var global = $"/root/Global"
var spd = 30
var grv = 9.8*3
var maxSpd = spd*2
var friction = 300
var AirRest = 1
var jumpForce = grv*25
var targetFps = 60
var canJump = true

# Called when the node enters the scene tree for the first time.
func _ready():
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
#	pass
	global.playerPos = self.global_transform
	movement(delta)

func movement(delta):
	var vel = Vector3.ZERO
	var inpX = 0
	var inpZ = 0
	
	if global.dimension == 2:
		self.translate(Vector3())
		inpX = Input.get_action_strength("left") - Input.get_action_strength("right")
		
		if inpX != 0:
			vel.z += inpX * spd * delta
			vel.z = clamp(vel.z, -maxSpd, maxSpd)
			
		vel.y += -grv * delta
		
		if Input.is_action_just_pressed("fowards") && canJump:
			vel.y = jumpForce * delta
			canJump = false
				
		#Is on Floor was not working.
		if is_on_wall():
			canJump = true
				
	if global.dimension == 3:
		inpX += Input.get_action_strength("right") - Input.get_action_strength("left")
		inpZ += Input.get_action_strength("backwards") - Input.get_action_strength("fowards")
		
		if inpX != 0:
			vel.x += inpX * spd * delta
			vel.x = clamp(vel.x, -maxSpd, maxSpd)
			

		if inpZ != 0:
			vel.z += inpZ * spd * delta
			vel.z = clamp(vel.z, -maxSpd, maxSpd)
		
		vel.y += -grv * delta
		
		if Input.is_action_just_pressed("jump") && canJump:
			vel.y = jumpForce * delta
			canJump = false
			
		if is_on_wall():
			canJump = true
			
			
	
	if global.lock3d == true:
		$Camera2D.current = true
		
	if Input.is_action_just_pressed("switch") && global.lock3d == false && global.lock2d == false:
		if global.dimension == 2:
			global.dimension = 3
			$Camera2D.current = false
		elif global.dimension == 3:
			global.dimension = 2
			$Camera3D.current = false
			
	if global.hurt == true:
		global.hurt = false
		global.health -= 1
		vel.y = jumpForce
	move_and_slide(vel * spd)

It's not godot specific but maybe check this video out from 15:35 onward:

It is not a problem with the camera, it still teleports, If I have the camera still, it still teleports but falls nicely.

"when all you have is a hammer, everything looks like a nail" - I guess since I had just discussed cameras and provided a resource with this video included in there, I was still thinking about cameras when seeing this topic. Still tho, what he shows in the video can be used for other things too, might help with some things perhaps.

As for the teleporting, I though that was something you were doing intentionally, however if not, I'd try debugging the collisions. For an example, your jumps might have too much force/acceleration to them and might be tunneling through the floors & ceilings.

The teleporting is that it moves instantly into the air not through the ceilings and floors.

velocity.y = jumpforce(grv(9,8 3) 25) delta, so your jump value is 735 delta pixels.

At 60 (physics, since you are using physics_process there) fps, that is 1000/60 = 16,666...67ms per frame, so a delta value of ~16,67... 735 16,67 = ~12252,45 pixels jumped. Unless I misrecall how delta was reported by the engine, it could also be 1000/60/1000 = 0,01666...67. That would make a significant difference and instead lower the value to 735 0,016 = 11,76 pixels per frame.

Either ways tho, since you are using kinematic_body and move_and_slide(velocity) you shouldn't multiply the jumpforce by delta when assigning it there and your jumpforce should probably just be equal to your intended jump height. If you intend it to be 12 just assign jumpforce the value 12.0.

Oh wait, I forgot to account for the spd value... so it's either 12252,45 30 or it's 11,76 30, probably too high either ways.

Anyways, you should probably check this page out: https://kidscancode.org/godot_recipes/basics/understanding_delta/ and especially: https://kidscancode.org/godot_recipes/basics/understanding_delta/#using-kinematic-functions

Removing delta didn't work but multiplying it into Input.get_action_strength(), worked, even though it prints one and zeros, when I try to print it and it also gives it a smooth froggy feeling, which may be good for the game? It also makes the game actually fun to move around in like a lot of other successful games. So in conclusion, you have to multiply it with a number that gradually turns to one to fix it.