User Tools

Site Tools


events:getting_started_with_godot_in_3d_part_2

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
events:getting_started_with_godot_in_3d_part_2 [2020/03/31 18:05]
admin
events:getting_started_with_godot_in_3d_part_2 [2020/03/31 18:18] (current)
admin
Line 7: Line 7:
   * add a script to weapon (default empty)   * add a script to weapon (default empty)
   * set up some variables for the weapon, track ammo code   * set up some variables for the weapon, track ammo code
-<​code>​+<​code ​python>
 export var fire_rate = 0.5 export var fire_rate = 0.5
 export var clip_size = 5 export var clip_size = 5
Line 20: Line 20:
  
 </​code>​ </​code>​
 +  * Test that out
  
 +<code python>
 +func _process(delta):​
 +  if Input.is_action_just_pressed("​primary_fire"​) and can_fire:
 +    print "Fired weapon"​
 +    can_fire = false
 +    ​
 +    yield(get_tree().create_timer(fire_rate),​ "​timeout"​)
 +    can_fire = true
  
 +</​code>​
 +
 +  * Test those changes (try changing the fire rate in the properties box)
 +  * Simulate reloading
 +<code python>
 +func _process(delta):​
 +  if Input.is_action_just_pressed("​primary_fire"​) and can_fire:
 +    if current_ammo > 0:
 +      print "Fired weapon"​
 +      can_fire = false
 +      current_ammo -= 1
 +    ​
 +      yield(get_tree().create_timer(fire_rate),​ "​timeout"​)
 +      can_fire = true
 +    else:
 +      print "​Reloading"​
 +      yield(get_tree().create_timer(reload_rate),​ "​timeout"​)
 +      current_ammo = clip_size
 +      print "​Reload complete"​
 +
 +</​code>​
 +  * Fix a reload issue (race condition):
 +<code python>
 +export var fire_rate = 0.5
 +export var clip_size = 5
 +export var reload_rate = 1
 +
 +var current_ammo = clip_size ​
 +var can_fire = true
 +var reloading = false
 +
 +func _process(delta):​
 +  if Input.is_action_just_pressed("​primary_fire"​) and can_fire:
 +    if current_ammo > 0 and not reloading:
 +      print "Fired weapon"​
 +      can_fire = false
 +      current_ammo -= 1
 +      ​
 +      yield(get_tree().create_timer(fire_rate),​ "​timeout"​)
 +      ​
 +      can_fire = true
 +    elif not reloading:
 +      print "​Reloading"​
 +      reloading = true
 +      yield(get_tree().create_timer(reload_rate),​ "​timeout"​)
 +      current_ammo = clip_size
 +      reloading = false
 +      print "​Reload complete"​
 +</​code>​
 +
 +  * Add a child node to the world, a static body, add a meshinstance to it as a child, called it enemy, make it a sphere
 +  * Add a child node to the staticbody, a collision shape sphere
 +  * Assign it to a group, (properties,​ node, "​enemies"​)
 +  * add new function:
 +<code python>
 +onready var raycast = $"​../​Head/​Camera/​RayCast"​
 +func check_collision():​
 +  if raycast.is_colliding():​
 +    var collider = raycast.get_collider()
 +     if collider.is_in_group("​enemies"​):​
 +       ​collider.queue_free()
 +       ​print("​Killed "​+collider.name)
 +</​code>​
events/getting_started_with_godot_in_3d_part_2.1585674355.txt.gz ยท Last modified: 2020/03/31 18:05 by admin