I created a player controller, but it does not react to the var "speed" at all, what is error?

vavengvaveng Posts: 1Member
edited July 23 in 2D

parents script

extends KinematicBody2D

class_name Actor

const FLOOR_NORMAL = Vector2.UP

var velocity: = Vector2.ZERO
var gravity = 2000
export var speed: = Vector2(500.0, 400.0)

player controller

extends Actor


func _physics_process(_delta: float) -> void:
    var direction = get_direction()
    velocity = calculate_move_velocity(velocity, direction, speed)
    velocity = move_and_slide(velocity, FLOOR_NORMAL)
func get_direction() -> Vector2:
    return Vector2(
        Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"), 
        -1.0 if Input.is_action_just_pressed("ui_up") && is_on_floor() else 0.0
    )

func calculate_move_velocity(
    linear_velocity: Vector2, 
    direction: Vector2, 
    speed: Vector2
    ) -> Vector2:
    var new_velocity: = linear_velocity
    new_velocity.x = speed.x * direction.x
    new_velocity.y += gravity * get_physics_process_delta_time()
    if direction.y == -1.0:
        new_velocity.y = speed.y * direction.y
    return new_velocity

Tags :

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 2,831Admin

    Welcome to the forums @vaveng!

    I'm just guessing here, but the issue may be because in calculate_move_velocity, one of the parameters is called speed. This would, for that function, make it override the class variable, causing speed not to be taken into account. I'd see if changing the name of that parameter fixes the issue.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file