#why is my vertical position sometimes only updating when im moving my camera ??(video)

10 messages · Page 1 of 1 (latest)

magic galleon
#
extends CharacterBody3D
@onready var head = $head



var SPEED = 5.0
const JUMP_VELOCITY = 4.5
const mouse_sens = 0.4

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")


func _physics_process(delta):
    # Add the gravity.
    if not is_on_floor():
        velocity.y -= gravity * delta
        

#hide your mouse 
func _ready():
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

---------------------PROBLEM (prob)------------------------------------------------------------
func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg_to_rad(-event.relative.x * mouse_sens))#this would rotate the whole "player" node left/right 
        head.rotate_x(deg_to_rad(-event.relative.y * mouse_sens)) #rotates only the head up/down
        head.rotation.x = clamp(head.rotation.x,deg_to_rad(-89),deg_to_rad(89))#limits how far you can look down
------------------------------------------------------------------------------
    # Handle jump.
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
    var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    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)

    move_and_slide()

its basically code from this video but more simple https://www.youtube.com/watch?v=xIKErMgJ1Yk

#

why is my vertical position sometimes only updating when im moving my camera ??(video)

#

this is not lag it just looks like that since everything moves vertically only when im moving the camera

plain hill
#

you have most of your movement logic in the _input method which is only called when an input event is received. this is why you only move when your mouse is moving (an input event). anything that does not need the event parameter should be in _physics_process instead of _input.
unrelated, but I would suggest using an exported var instead of a const for the speed, jump velocity, and mouse sensitivity so that you can easily tweak them from the inspector

magic galleon
#

Idk why did it work for him but I might copied it wrong

magic galleon
#

I mean I could but does not explain why this does not work

#

the actual problem is

-------------------------works------------------------------------------------------------
func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg_to_rad(-event.relative.x * mouse_sens))
        head.rotate_x(deg_to_rad(-event.relative.y * mouse_sens)) 
        head.rotation.x = clamp(head.rotation.x,deg_to_rad(-89),deg_to_rad(89))    
    

# Add the gravity.
func _physics_process(delta):
    if not is_on_floor():
        velocity.y -= gravity * delta    
-----------------------------------------------------------------------------------------
---------------------does not work------------------------------------------------------
# Add the gravity.
func _physics_process(delta):
    if not is_on_floor():
        velocity.y -= gravity * delta    


func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg_to_rad(-event.relative.x * mouse_sens))
        head.rotate_x(deg_to_rad(-event.relative.y * mouse_sens)) 
        head.rotation.x = clamp(head.rotation.x,deg_to_rad(-89),deg_to_rad(89))
--------------------------------------------------------------------------------------
plain hill
#

what is the difference between the two code blocks above? everything from # Handle jump to the end in your original post should be moved to _physics_process