Advertisement
metalx1000

Godot Basic Platform Player Example

Aug 23rd, 2021
3,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. #Godot Basic Platform Player Script
  4. #Copyright (C) 2021  Kristofer Occhipinti - Films By Kris
  5. #http://filmsbykris.com
  6.  
  7. #This program is free software: you can redistribute it and/or modify
  8. #it under the terms of the GNU General Public License as published by
  9. #the Free Software Foundation version 3 of the License.
  10. #
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #GNU General Public License for more details.
  15. #
  16. #You should have received a copy of the GNU General Public License
  17. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. export (int) var speed = 500
  20. export (int) var jump_speed = -500
  21. export (int) var gravity = 800
  22.  
  23. export (float, 0, 1.0) var friction = 0.1
  24. export (float, 0, 1.0) var acceleration = 0.25
  25.  
  26. var velocity = Vector2.ZERO
  27. onready var sprite = $Sprite
  28.  
  29. func get_input():
  30.     walk()
  31.     jump()
  32.    
  33. func walk():
  34.     var dir = 0
  35.     if Input.is_action_pressed("walk_right"):
  36.         dir += 1
  37.         sprite.flip_h = 0
  38.     if Input.is_action_pressed("walk_left"):
  39.         dir -= 1
  40.         sprite.flip_h = 1
  41.     if dir != 0:
  42.         velocity.x = lerp(velocity.x, dir * speed, acceleration)
  43.     else:
  44.         velocity.x = lerp(velocity.x, 0, friction)
  45.    
  46. func jump():
  47.     if Input.is_action_just_pressed("jump"):
  48.         if is_on_floor():
  49.             velocity.y = jump_speed
  50.        
  51. func animation():
  52.     if velocity.x > 1 || velocity.x < -1:
  53.         sprite.play("walk")
  54.     else:
  55.         sprite.play("stand")
  56.        
  57. func _physics_process(delta):
  58.     get_input()
  59.     animation()
  60.     velocity.y += gravity * delta
  61.     velocity = move_and_slide(velocity, Vector2.UP)
  62.    
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement