Topic was automatically imported from the old Question2Answer platform.
Asked By
psear
At the minute I have a simple 2D setup. There is a function called in _process() to handle inputs. If it detects a jump key, I use apply_central_impulse() to make the rigidbody move upwards and fall as normal, and this works fine. I want to make it so that pressing right, for example, makes the character move right. The problem is, I don’t want to apply a force, because the movement is slow at first as it accelerates. The character should move at a speed when pressed, and not move at all when the key is not held. How do I achieve this? The docs warn against directly changing the velocity of a rigidbody directly.
in there is linear_velocity … and thats what you’re searching for
I have tried something like this:
func _process(delta):
manage_inputs()
func manage_inputs():
var moving = false
if Input.is_action_just_pressed("ui_up"):
if linear_velocity.y == 0:
jump()
if Input.is_action_pressed("ui_right"):
move("right")
moving = true
if Input.is_action_pressed("ui_left"):
move("left")
moving = true
if !moving:
vel_linear.x = 0
func jump():
apply_central_impulse(Vector2(0, -1000))
func move(direction):
if direction == "right":
vel_linear.x = 400
if direction == "left":
vel_linear.x = -400
func _integrate_forces(state):
state.linear_velocity.x = vel_linear.x
But doing things in this way causes the character to sporadically freeze when moving continuously. What’s up with that? Are there any alternative solutions?
psear | 2020-08-22 11:04
Hi,
i’ve just tested it … runs fine by me. Must be something else. Perhaps the surface the body glides on?