Godot 4 GDScript 3D Player Movement and Jumping Code - Tutorial
extends KinematicBody3D\n\nvar speed:float = 5\nvar gravity:float = -9.8\nvar jumpForce:float = 8\nvar velocity:Vector3\nvar isJumping:bool = false\n\nfunc _physics_process(delta:float) -> void:\n # Player Movement\n var moveDir:Vector3 = Vector3.ZERO\n moveDir.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")\n moveDir.z = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")\n moveDir = moveDir.normalized()\n move_dir.y = 0\n move_dir = move_dir.rotated(Vector3.UP, deg2rad(45))\n velocity.x = move_dir.x * speed\n velocity.z = move_dir.z * speed\n\n # Jumping\n if Input.is_action_just_pressed("ui_accept") and is_on_floor():\n velocity.y = jumpForce\n isJumping = true\n\n # Gravity\n if not is_on_floor():\n velocity.y += gravity * delta\n\n # Applying Movement and Gravity\n velocity = move_and_slide(velocity, Vector3.UP)\n if isJumping and is_on_floor():\n isJumping = false\n velocity.y = 0\n\n # Rotating Player to Face Movement Direction\n if moveDir != Vector3.ZERO:\n rotation.y = atan2(moveDir.x, moveDir.z)\n\n # Applying Movement\n move_and_slide(velocity, Vector3.UP)
原文地址: http://www.cveoy.top/t/topic/psIO 著作权归作者所有。请勿转载和采集!