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)