Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- var max_speed = 60
- var gravity = 120
- var jump_impulse = 55
- var jump = false
- var sprite_node
- var velocity = Vector3.ZERO
- func _ready():
- sprite_node = get_node("Sprite3D")
- func _physics_process(delta):
- var input_vector = get_input_vector()
- apply_movement(input_vector)
- apply_gravity(delta)
- jump()
- velocity = move_and_slide(velocity, Vector3.UP)
- func get_input_vector():
- var input_vector = Vector3.ZERO
- input_vector.x = int(Input.is_action_pressed("move_right")) - int(Input.is_action_pressed("move_left"))
- input_vector.z = int(Input.is_action_pressed("move_back")) - int(Input.is_action_pressed("move_forward"))
- return input_vector.normalized()
- func apply_movement(input_vector):
- if is_on_floor(): #this command is_on_floor and jump == false prevents--
- jump == false #--- the player moving left-right in mid-air
- velocity.x = input_vector.x * max_speed
- velocity.z = input_vector.z * max_speed
- if velocity.x > 0: #setting a variable "sprite_node" enables
- sprite_node.set_flip_h(false) #the sprite to be called and flipped
- elif velocity.x < 0: # in the command line
- sprite_node.set_flip_h(true)
- func apply_gravity(delta):
- velocity.y -= gravity * delta
- func jump():
- if is_on_floor() and Input.is_action_just_pressed("jump"):
- jump = true
- velocity.y = jump_impulse
Advertisement
Add Comment
Please, Sign In to add comment