{"id":29,"date":"2024-08-24T13:57:34","date_gmt":"2024-08-24T13:57:34","guid":{"rendered":"https:\/\/opensourcegamers.com\/wp-blog\/?p=29"},"modified":"2024-08-24T14:05:28","modified_gmt":"2024-08-24T14:05:28","slug":"how-to-make-a-simple-3d-player-in-godot-4-0","status":"publish","type":"post","link":"https:\/\/opensourcegamers.com\/wp-blog\/2024\/08\/24\/how-to-make-a-simple-3d-player-in-godot-4-0\/","title":{"rendered":"How To Easily Make a 3d Player In Godot 4.0"},"content":{"rendered":"\n<p class=\"has-text-align-center\">Making A Player Character is one of the most Important things to do in ANY game development process. Here is how to do it in both 3D and 2D<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-text-align-center\">There are a couple steps we will do for this character.<\/p>\n\n\n\n<p>Step One: Node Tree<\/p>\n\n\n\n<p>To Start, we need to make the correct setup for the character. the node tree goes like this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CharacterBody3d\n<ul class=\"wp-block-list\">\n<li>MeshInstance3d<\/li>\n\n\n\n<li>CollisionShape3d<\/li>\n\n\n\n<li>Node3d (call this node &#8220;Head&#8221;)\n<ul class=\"wp-block-list\">\n<li>Camera3d<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>For the mesh instance 3d, set the shape to the weird pill shape thing<\/p>\n\n\n\n<p>For the Collision Shape 3d, select that shape as well.<\/p>\n\n\n\n<p>ok. now its time to map the controls<\/p>\n\n\n\n<p>To start, click on the label called project at the top of godot<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"70\" src=\"https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-055347.png\" alt=\"\" class=\"wp-image-11\" srcset=\"https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-055347.png 326w, https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-055347-300x64.png 300w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><\/figure>\n\n\n\n<p>click on project settings, then on input map<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"71\" src=\"https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-055415.png\" alt=\"\" class=\"wp-image-12\" srcset=\"https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-055415.png 372w, https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-24-055415-300x57.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/figure>\n\n\n\n<p>Finally, set it up like this.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"870\" height=\"322\" src=\"https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"\" class=\"wp-image-10\" srcset=\"https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/image.png 870w, https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/image-300x111.png 300w, https:\/\/opensourcegamers.com\/wp-blog\/wp-content\/uploads\/2024\/08\/image-768x284.png 768w\" sizes=\"auto, (max-width: 870px) 100vw, 870px\" \/><\/figure>\n\n\n\n<p>Now, time for the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extends CharacterBody3D\n\nconst SPEED = 200\nconst JUMP_VELOCITY = 20\nconst SENSITIVITY = .001\n#Get the gravity from the project settings to be synced with RigidBody nodes.\nvar gravity = ProjectSettings.get_setting(\"physics\/3d\/default_gravity\")\n@onready var head = $Head\n@onready var camera = $Head\/Camera3D\n\nfunc _ready():\n\tInput.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)\n\nfunc _unhandled_input(event):\n\tif event is InputEventMouseMotion:\n\t\thead.rotate_y(-event.relative.x * SENSITIVITY)\n\t\tcamera.rotate_x(-event.relative.y * SENSITIVITY)\n\t\tcamera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))\n\nfunc _physics_process(delta):\n\t# Add the gravity.\n\tif not is_on_floor():\n\t\tvelocity.y -= gravity * delta\n\n\t# Handle jump.\n\tif Input.is_action_just_pressed(\"move_jump\"):\n\t\tvelocity.y = JUMP_VELOCITY\n\n\t# Get the input direction and handle the movement\/deceleration.\n\t# As good practice, you should replace UI actions with custom gameplay actions.\n\tvar input_dir = Input.get_vector(\"move_left\", \"move_right\", \"move_back\", \"move_forward\")\n\n#Calculate the forward and right directions based on the camera orientation.\n\tvar forward_dir = -camera.global_transform.basis.z\n\tvar right_dir = camera.global_transform.basis.x\n\n#Calculate the direction the player should move.\n\tvar direction = (forward_dir * input_dir.y + right_dir * input_dir.x).normalized()\n\n\tif direction:\n\t\tvelocity.x = direction.x * SPEED\n\t\tvelocity.z = direction.z * SPEED\n\telse:\n\t\tvelocity.x = move_toward(velocity.x, 0, SPEED)\n\t\tvelocity.z = move_toward(velocity.z, 0, SPEED)\n\n\tmove_and_slide()\n<\/code><\/pre>\n\n\n\n<p>Attach this as a script to the character body 3d node. I will now break the code down for you.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>First, to modify any player attributes, modify the constants at the top<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const SPEED = 200\nconst JUMP_VELOCITY = 20\nconst SENSITIVITY = .001<\/code><\/pre>\n\n\n\n<p>Speed = Player Speed<\/p>\n\n\n\n<p>Jump_Velocity = how high the player jumps<\/p>\n\n\n\n<p>Sensitivity = Mouse Sensitivity<\/p>\n\n\n\n<p>thats actually all you need to know to use this nvm sorry lol<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making A Player Character is one of the most Important things to do in ANY game development process. Here is how to do it in both 3D and 2D There are a couple steps we will do for this character. Step One: Node Tree To Start, we need to make the correct setup for the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_siteseo_robots_primary_cat":"1","footnotes":""},"categories":[1],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":3,"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/posts\/29\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opensourcegamers.com\/wp-blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}