Godot Version
Godot 4.5
Question
So, my players jump in an arc, that i navigate by mouse. I want to predict time for my player jumping, but i have an error of 0.05-0.2 s, and this is kinda big. Are there ways to minimize that error?
There’s code for my player movement and timer, that checks actual time of jump
func _physics_process(delta: float) -> void:
if not is_on_floor():
velocity.y += gravity*delta
else:
jumping = false
if Input.is_action_pressed("click") && is_on_floor():
lineTrajectory._update_trajectory(head.global_transform.x.y * jump_strength, get_gravity().y, delta, head.global_transform.x.x * speed, global_position)
jumping = true
velocity.x = 0
if Input.is_action_just_released("click"):
lineTrajectory._refresh()
if is_on_floor():
timer.paused = false
timer.start(-1)
jumping = true
velocity.y += head.global_transform.x.y * jump_strength
velocity.x += head.global_transform.x.x * speed
var direction = Input.get_axis("left","right")
if jumping == false:
if not timer.is_stopped():
timer.paused = true
$"real time".text = 'Real time: ' + str(timer.wait_time - timer.time_left)
timer.stop()
timer.wait_time = 50.0
if direction:
velocity.x = direction*speed
else:
velocity.x = move_toward(velocity.x, 0, 60)
head.look_at(get_global_mouse_position())
move_and_slide()
And there’s code for my trajectory line
extends Line2D
@onready var character = get_parent().get_node("CharacterBody2D")
@onready var collisiontest = $CollisionTest
@export var max_points = 300
var linePosition = Vector2.ZERO
var collision = null
func _ready() -> void:
_refresh()
func _refresh():
clear_points()
linePosition = character.global_position
collisiontest.velocity = Vector2.ZERO
for i in max_points:
add_point(linePosition)
collisiontest.position = linePosition
collision = null
visible = false
func get_time(start_position: Vector2, end_position: Vector2, velocity: Vector2):
var distance_vector = end_position - start_position
if velocity.length() > 0:
var time: float = distance_vector.length() / velocity.length()
return time
else:
return 0.0
func _update_trajectory(jumpForce, gravity, delta, Xvel, startingPosition):
linePosition = startingPosition
collisiontest.position = linePosition
collisiontest.velocity.y = jumpForce
collisiontest.velocity.x = Xvel
var time = 0.0
for i in get_point_count():
var start = collisiontest.position
#if !collision:
collisiontest.velocity.y += gravity * delta
if collisiontest.is_on_floor():
collisiontest.velocity.x = move_toward(collisiontest.velocity.x, 0, 60)
set_point_position(i, collisiontest.position)
collision = collisiontest.move_and_collide(collisiontest.velocity * delta, false, 0)
if collision:
collisiontest.velocity = collisiontest.velocity.bounce(collision.get_normal()) * 0.0
var end = collisiontest.position
time += get_time(start, end, collisiontest.velocity)
character.predicted.text = 'Predicted time ' + str(time)
visible = true
Sry for bad eng, not my first language.