User Tools

Site Tools


events:getting_started_with_godot_in_3d

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
events:getting_started_with_godot_in_3d [2020/03/30 18:10]
admin created
events:getting_started_with_godot_in_3d [2020/03/31 18:04] (current)
admin
Line 1: Line 1:
 +====== Getting Started with Godot in 3D ======
 ==== Notes for everyone ===== ==== Notes for everyone =====
   * Create 3D spatial node, called World   * Create 3D spatial node, called World
   * Add child node, create KinematicBody,​ called Player   * Add child node, create KinematicBody,​ called Player
   * Add child node to player, type Collision Shape, type capsule   * Add child node to player, type Collision Shape, type capsule
-  * Make vertical. ​ Transform x by 90 degrees. ​ Set radius to 0.75, height to 1.25. +  * Make vertical. ​ Transform x by 90 degrees.  ​ 
-  * +  * Click capsule shape dropdown, ​Set radius to 0.75, height to 1.25. 
 +  * To player, add child node, type spatial, name it head 
 +  * Add child node to head, type camera, move it to around 0.9 on the Y axis 
 +  * Add a child to the player node, add meshinstance with shapre capsule 
 +  * Size the capsule to the collision mesh - click on capusulemesh then 0.75 radius, height 1.25 and transform rotate 90 on x 
 +  * Add new child now to World, MeshInstance,​ CubeMesh 
 +  * drag below player, then scale to a floor size (20x20 or whatever) 
 +  * Click, Mesh, create Trimesh static body 
 +  * Save Player as Scene 
 +  * Enter Player scene 
 +  * edit input map (Project -> Project Settings -> Input Map) 
 +  * add move_forward,​ move_backward,​ move_left, move_right and jump 
 +  * assign keys (WASD, space) 
 +  * Right Click player, attach script, default, template empty 
 +<code python>​ 
 +extends KinematicBody 
 + 
 +export var speed = 10 
 +export var acceleration = 5 
 +export var gravity = 0.98 
 +export var jump_power = 30 
 + 
 +onready var head = $Head 
 +onready var camera = $Head/​Camera 
 + 
 +var velocity = Vector3() 
 + 
 +func _physics_process(delta):​ 
 +  
 + var head_basis = head.get_global_transform().basis 
 +  
 + var direction = Vector3() 
 + if Input.is_action_pressed("​move_forward"​):​ 
 + direction -= head_basis.z 
 + elif Input.is_action_pressed("​move_backward"​):​ 
 + direction += head_basis.z 
 +  
 + if Input.is_action_pressed("​move_left"​):​ 
 + direction -= head_basis.x 
 + elif Input.is_action_pressed("​move_right"​):​ 
 + direction += head_basis.x 
 + 
 + direction = direction.normalized() 
 + velocity = velocity.linear_interpolate(direction * speed, acceleration * delta) 
 +  
 + velocity = move_and_slide(velocity) 
 + 
 +</​code>​ 
 +  * Test the above code out, now lets add some camera/head controls 
 +<code python>​ 
 +extends KinematicBody 
 + 
 +export var speed = 10 
 +export var acceleration = 5 
 +export var gravity = 0.98 
 +export var jump_power = 30 
 +export var mouse_sensitivity = 0.3 
 + 
 +onready var head = $Head 
 +onready var camera = $Head/​Camera 
 + 
 +var velocity = Vector3() 
 +var camera_x_rotation = 0 
 + 
 +func _input(event):​ 
 + if event is InputEventMouseMotion:​ 
 + head.rotate_y(deg2rad(-event.relative.x * mouse_sensitivity)) 
 + var x_delta = event.relative.y * mouse_sensitivity 
 + if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta < 90: 
 + camera.rotate_x(deg2rad(-x_delta)) 
 + camera_x_rotation += x_delta 
 + 
 +func _physics_process(delta):​ 
 +  
 + var head_basis = head.get_global_transform().basis 
 +  
 + var direction = Vector3() 
 + if Input.is_action_pressed("​move_forward"​):​ 
 + direction -= head_basis.z 
 + elif Input.is_action_pressed("​move_backward"​):​ 
 + direction += head_basis.z 
 +  
 + if Input.is_action_pressed("​move_left"​):​ 
 + direction -= head_basis.x 
 + elif Input.is_action_pressed("​move_right"​):​ 
 + direction += head_basis.x 
 + 
 + direction = direction.normalized() 
 + velocity = velocity.linear_interpolate(direction * speed, acceleration * delta) 
 +  
 + velocity = move_and_slide(velocity) 
 + 
 +</​code>​ 
 +  * Add gravity. ​ Oops. 
 +<code python>​ 
 +velocity.y += gravity 
 +</​code>​ 
 +  * Add jumping 
 +<code python>​ 
 +if Input.is_action_just_pressed("​jump"​):​ 
 + velocity.y += jump_power 
 +</​code>​ 
 +  * Define the floor to fix jumping 
 +<code python>​ 
 + if Input.is_action_just_pressed("​jump"​) and is_on_floor():​ 
 + velocity.y += jump_power 
 +  
 + velocity = move_and_slide(velocity,​ Vector3.UP) 
 +</​code>​ 
 +  * Lock the mouse to the window 
 +<code python>​ 
 +func _ready(): 
 + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 
 + 
 +func _process(delta):​ 
 + if Input.is_action_just_pressed("​ui_cancel"​):​ 
 + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) 
 +</​code>​ 
 + 
 +  * I think that's as far as we'll get in the first session, I'll make a start on [[Getting started with Godot in 3D Part 2|part two]] though.
  
 ==== Notes for me ===== ==== Notes for me =====
  
 +  * Axis, blue is Z (left to right generally), green is Y (up and down), X is red, back and forward (depth)
   * Explain viewports:   * Explain viewports:
     * move mode:     * move mode:
Line 18: Line 138:
     * mouse wheel zoom     * mouse wheel zoom
     * F to focus on object, O to focus on origin     * F to focus on object, O to focus on origin
-    *  +    * right mouse, WASD 
-  *  +  * Ctrl + Left click for inline help 
-  * + 
 +==== Further reading/​viewing ==== 
 +  * This tutorial is based on [[https://​www.youtube.com/​channel/​UCdU9e4eNsJif0rBrBiYRb5g|Code with Tom'​s]] Youtube series [[https://​www.youtube.com/​watch?​v=UV-bhtb3734|Make and FPS in Godot]]. ​ Part two is [[https://​www.youtube.com/​watch?​v=Y_2oiLjOx54|here]]. 
 +  * [[https://​www.youtube.com/​watch?​v=49awsu1VJbo|Building 3D levels]] 
events/getting_started_with_godot_in_3d.1585588241.txt.gz · Last modified: 2020/03/30 18:10 by admin