0 votes
extends KinematicBody

onready var pivot: Position3D = $Pivot

export var air_friction = 2
export var friction = 9
export var speed := 10.0
export var jump_strength := 20.0
export var gravity := 40
export var acel = 70
var _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN
var control_sens = 0.8
var move_direction = Vector3.ZERO



func _ready():
    print(pivot)



func _physics_process(delta):



    move_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
    move_direction.z = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
    var direction = get_direction(move_direction)
    apply_movement(move_direction, direction, delta)
    _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)


    apply_friction(direction, delta)
func get_direction(move_direction):
    var direction = (move_direction.x * CamScript.transform.basis.x) + (move_direction.z * CamScript.transform.basis.z)
    return direction

func _process(delta):
    pass



func apply_movement(move_direction, direction, delta):
    if direction != Vector3.ZERO:
        _velocity.x = _velocity.move_toward(direction * speed, acel * delta).x
        _velocity.z = _velocity.move_toward(direction * speed, acel * delta).z
        pivot.look_at(global_transform.origin + direction, Vector3.UP)

func apply_friction(direction, delta):
    if direction == Vector3.ZERO:
        if is_on_floor():
            _velocity = _velocity.move_toward(Vector3.ZERO, friction * delta)
        else:
            _velocity.x = _velocity.move_toward(direction * speed, air_friction * delta).x
            _velocity.z = _velocity.move_toward(direction * speed, air_friction * delta).z

screenshot of scene

Godot version 3.5.1
in Engine by (12 points)

1 Answer

0 votes

Cause

This can mean only one thing your pivot var has not been properly set when you try to access it

Solution

if pivot:
    pivot.look_at(global_transform.origin + direction, Vector3.UP)

10 Cents Advice

You actually don't need a node for this and can offset that position from the transform

var pivot = global_transform.basis.z + offset
by (6,876 points)
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.