Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | yellowJUSTice |
basically whenever i press the left or right button its supposed to add acceleration and eventually get to max speed and when i let go its supposed to stop however its not working like that whenever i press it it stays on only the assign acceleration speed and whenever i click the left/right button acceleration is added its confusing.
script
extends KinematicBody2D
var MAX_SPEED = 500
var ACCELERATION = 5
var DASH_ACCELERATION = 500
var GRAVITY = 40
var DASH_STOP = 0
var JUMP_FORCE = 800
var JUMP_RELEASE_FORCE = 40
var FRICTION = 10
var velocity = Vector2.ZERO
func _physics_process(_delta):
velocity.y += GRAVITY
if velocity.length() > MAX_SPEED:
velocity = velocity.normalized() * MAX_SPEED
velocity = move_and_slide(velocity)
if Input.is_action_just_pressed("ui_right"):
if Input.is_action_just_pressed("ui_right"):
velocity.x += ACCELERATION
if Input.is_action_just_pressed("ui_left"):
velocity.x -= ACCELERATION
if Input.is_action_just_pressed("ui_up"):
velocity.y -= JUMP_FORCE
if Input.is_action_just_pressed("dash"):
velocity.x += DASH_ACCELERATION
if Input.is_action_just_released("dash"):
velocity.x = DASH_STOP
func accelerate(direction: Vector2):
velocity = velocity.move_toward(MAX_SPEED * direction, ACCELERATION)
func apply_friction():
velocity = velocity.move_toward(Vector2.ZERO, FRICTION)
Edited to fix forum code formatting. Though, as posted, the duplicate code and incorrect indentions here look suspect…
if Input.is_action_just_pressed("ui_right"):
if Input.is_action_just_pressed("ui_right"):
velocity.x += ACCELERATION
jgodfrey | 2023-05-24 22:00