0 votes

Basically, I can't seem to get acceleration and deceleration to work on my kinematic body at all. I'm really new to this, so maybe it's obvious, but in theory what I've written should work? don't know why it isn't.

code:

extends KinematicBody

export var speed := 30.0
export var jump_strength := 25.0
export var gravity := 70.0
export var rotation_speed := 20.0
export var acceleration := 45.0
export var friction := 35.0



var _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN
var _linear_velocity = Vector3()

onready var _spring_arm: SpringArm = $SpringArm
onready var _model: Spatial = $COOLGUYbeta


func _physics_process(delta: float) -> void:
    var move_direction := Vector3.ZERO

    var _vv = _linear_velocity.y # Vertical velocity.
    var _hv = Vector3(_linear_velocity.x, 0, _linear_velocity.z) # Horizontal velocity.

    var _hdir = _hv.normalized() # Horizontal direction.
    var _hspeed = _hv.length() # Horizontal speed.


    move_direction.x = Input.get_action_strength("right") - Input.get_action_strength("left")

    move_direction.z = Input.get_action_strength("back") - Input.get_action_strength("forward")

    move_direction = move_direction.rotated(Vector3.UP, _spring_arm.rotation.y).normalized()

    if is_on_floor():
        if _hspeed < speed:
            _hspeed += acceleration * delta

        else:
            _hspeed -= friction * delta
            if _hspeed < 0:
                _hspeed = 0



    print(_hspeed)


    _velocity.x = move_direction.x * speed
    _velocity.z = move_direction.z * speed
    _velocity.y -= gravity * delta





    var just_landed := is_on_floor() and  _snap_vector == Vector3.ZERO
    var is_jumping := is_on_floor() and Input.is_action_just_pressed("jump")
    if is_jumping:
        _velocity.y = jump_strength
        _snap_vector = Vector3.ZERO
    elif just_landed:
        _snap_vector = Vector3.DOWN
    _velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
    if Vector2(_velocity.z, _velocity.x).length() > 0.2:
        var look_direction = Vector2(_velocity.z, _velocity.x)
        _model.rotation.y = lerp_angle(_model.rotation.y, look_direction.angle(), delta * rotation_speed)


func _process(_delta: float) -> void:
    _spring_arm.translation = translation
Godot version 3.5
in Engine by (15 points)

1 Answer

+1 vote
Best answer

You're not even using _hspeed, you're just setting the velocity to speed (which should be named max_speed, maybe that would have helped you not confuse them).

by (1,244 points)
selected by

so i changed this:

_velocity.x = move_direction.x * speed
    _velocity.z = move_direction.z * speed
    _velocity.y -= gravity * delta

to this:

_velocity.x = move_direction.x * _hspeed
    _velocity.z = move_direction.z * _hspeed
    _velocity.y -= gravity * delta

thinking that that was the issue but now it just moves at a fixed rate that is determined by my acceleration variable. dunno exactly what i should do instead. but i do appreciate the help regardless, pointing me in the right direction

What do you mean "fixed rate"? Is it not increasing ip to max speed?

Edit: nevermind, found your issue. You have two different velocity variable. You are getting the speed from one (which is always at zero) and then setting the other. Just use velocity and remove linear velocity.

nope. it just starts and stays at the value determined by my acceleration variable.

I've edited my comment, I think I've found the issue.

hahahaha yep that was it! thanks. the dangers of referencing two separate examples of movement code. I really appreciate the help!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.