Advertisement
Guest User

PlayerKinematicBody2D

a guest
Feb 22nd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using Godot;
  2. using System;
  3.  
  4. public class Invee : KinematicBody2D {
  5.    
  6.     private AnimatedSprite player_sprite;
  7.     private Vector2 _vel = new Vector2();
  8.  
  9.     private string _animState = "idle";
  10.     private int _gravity = 15;
  11.     private int _walkSpeed = 100;
  12.     private int _jumpHeight = 400;
  13.  
  14.     public override void _Ready() {
  15.         player_sprite = (AnimatedSprite)GetNode("AnimatedSprite");
  16.         player_sprite.Play(_animState);
  17.     }
  18.  
  19.  
  20.     public override void _PhysicsProcess(float delta) {
  21.         _vel.y += _gravity;
  22.         _vel.x = 0;
  23.  
  24.         if (Input.IsKeyPressed((int) KeyList.D)) {_vel.x = _walkSpeed;}
  25.         else if(Input.IsKeyPressed((int) KeyList.A)) {_vel.x = -_walkSpeed;}
  26.  
  27.         if (_vel.x > 0) {player_sprite.FlipH = false;}
  28.         else if (_vel.x < 0) {player_sprite.FlipH = true;}
  29.  
  30.         //player_sprite.FlipH = (_vel.x > 0) ? false: (_vel.x < 0) ? true: false;
  31.  
  32.         if (IsOnFloor()) {
  33.             if (Input.IsKeyPressed((int) KeyList.Space)) {
  34.                 _vel.y = -_jumpHeight;
  35.             }
  36.         }
  37.  
  38.         if (_vel.x == 0) {_animState = "idle";}
  39.         else {_animState = "run";}
  40.  
  41.         if (player_sprite.Animation != _animState) {
  42.             player_sprite.Play(_animState);
  43.         }
  44.         _vel = MoveAndSlide(_vel, Vector2.Up);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement