Godot Version
4.3
Question
I need this in order to make a dash from the spot. Here is my code
player.gd
extends Unit
@onready var head = $Head
@export var VelocityComponent : _VelocityComponent
@export var ControllerComponent : _ControllerComponent
func _ready() -> void:
ControllerComponent.Camera = $Head/Camera3D
ControllerComponent.Head = head
VelocityComponent.ControllerComponent = ControllerComponent
func _physics_process(delta: float) -> void:
VelocityComponent.Move(self)
VelocityComponent.gd
extends Node
class_name _VelocityComponent
@onready var TimerStamina := $RecoverStamina
var ControllerComponent : _ControllerComponent
var Velocity : Vector3
var TransformBasis : Basis
var SPEED : int
var Player : Unit
var direction := Vector3.ZERO
const WALL_FRICTION = 0.5
const FLY_MOVE_MULTIPLIER = 2.0
@export var DASH_MULTIPLIER := 15
@export var jump_height : float
@export var jump_time_to_peak : float
@export var jump_time_to_descent : float
@onready var jump_velocity : float = (2.0 * jump_height) / jump_time_to_peak
@onready var jump_gravity : float = (-2.0 * jump_height) / (jump_time_to_peak * jump_time_to_peak)
@onready var fall_gravity : float = (-2.0 * jump_height) / (jump_time_to_descent * jump_time_to_descent)
func _physics_process(delta: float) -> void:
var input_dir := ControllerComponent.input_dir
direction = (TransformBasis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if Player.is_on_floor():
if direction:
Velocity.x = direction.x * SPEED
Velocity.z = direction.z * SPEED
else:
Velocity.x = move_toward(Velocity.x, 0, SPEED)
Velocity.z = move_toward(Velocity.z, 0, SPEED)
else:
Velocity.x = lerp(Velocity.x, direction.x*SPEED,delta * FLY_MOVE_MULTIPLIER)
Velocity.z = lerp(Velocity.z, direction.z*SPEED,delta * FLY_MOVE_MULTIPLIER)
Velocity.y += Gravity() * delta
Friction()
func RecoverStamina():
if Player.stamina < Player.MAX_STAMINA:
Player.stamina += Player.COUNT_RECOVER_STAMINA
TimerStamina.start()
func WasteStamina(num : int):
if Player.stamina > Player.MIN_STAMINA:
Player.stamina -= num
TimerStamina.start()
func Jump():
if Player.stamina > Player.MIN_STAMINA:
if Player.is_on_floor():
Velocity.y = jump_velocity
if !Player.is_on_floor() and Player.is_on_wall():
Velocity = Player.get_wall_normal() * jump_velocity
Velocity.y = jump_velocity
func Gravity() -> float:
return jump_gravity if Velocity.y > 0.0 else fall_gravity
func Friction():
if Player.is_on_wall() and !Player.is_on_floor():
Velocity.y *= WALL_FRICTION
func Move(player : Unit):
Player = player
player.velocity = Velocity
TransformBasis = player.head.transform.basis
SPEED = player.speed
player.move_and_slide()