Hello! I'm a greenie. I was trying to figure out how to get some nice feeling movement in my new project, but I just can't get the dang thang to work! I've tried everything short of what is currently out of my knowledge scope for vector math and quats. I've got it to turn towards the walk direction but that's about it... Any help would be much appreciated! c:
Here is my player node tree if that needed.
``
My player script so far:
extends KinematicBody
export var _gravity := 30.0
export var _jump_strength := 10.0
export var _rotation_speed := 10.0
export var _acceleration_on := false
export var _speed := 6.0
export(float, 0.05, 1.0) var tilt := 0.3
#### Cache children needed for later later
onready var _spring_arm: SpringArm = $SpringArm
onready var _player: Spatial = $"."
onready var _player_mesh: Spatial = $"lifter/astro-boy"
#### Variables for velocity vecotor
#### The snap variable is needed for slopes
var _snap_vector := Vector3.DOWN
var _acceleration_grav : Vector3
var _velocity := Vector3.ZERO
var _last_velocity := Vector3.ZERO
var _move_direction := Vector3.ZERO
var _acceleration : Vector3
#### Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
func _process(_delta: float) -> void:
#### Translate sping arm to character on each frame as it will not be a child of the player
_spring_arm.translation = translation
#### Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
move(delta)
jump(delta)
#### Called to calculate variaous player movements
func move(delta) -> void:
#### Set variable for direction vector
var look_direction = Vector2(_velocity.z, _velocity.x)
var last_collision = get_last_slide_collision()
_last_velocity = _velocity
#### Get input for movment
_move_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
_move_direction.z = Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
_move_direction = _move_direction.rotated(Vector3.UP, _spring_arm.rotation.y).normalized()
#### Calculate movement acceleration to get to top _speed and slow down from top _speed
if is_on_floor():
if _move_direction != Vector3.ZERO:
_velocity = _velocity.linear_interpolate(_move_direction * _speed, 5 * delta)
else:
_velocity = _velocity.linear_interpolate(Vector3.ZERO, 10 * delta)
_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
else:
if last_collision == null:
_velocity.y -= _gravity * delta
_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
#### Calculate rotaion of player towards velocity
if look_direction.length() > 0.2:
_player.rotation.y = lerp_angle(_player.rotation.y, look_direction.angle(), delta * _rotation_speed)
#### Called to calculate jumping
func jump(_delta):
#### Set variable for jumping and landing states
var just_landed := is_on_floor() and _snap_vector == Vector3.ZERO
var is_jumping := is_on_floor() and Input.is_action_just_pressed("jump")
#### Test to see if player is jumping or landed
if is_jumping:
_velocity.y = _jump_strength
_snap_vector = Vector3.ZERO
elif just_landed:
_snap_vector = Vector3.DOWN
return is_jumping