Guest User

Untitled

a guest
Mar 15th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. using Godot;
  2. using System;
  3.  
  4. public partial class Character : CharacterBody3D
  5. {
  6. public const float Speed = 5.0f;
  7. public const float JumpVelocity = 4.5f;
  8.  
  9. // Get the gravity from the project settings to be synced with RigidBody nodes.
  10. public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
  11.  
  12. public override void _PhysicsProcess(double delta)
  13. {
  14. Vector3 velocity = Velocity;
  15.  
  16. // Add the gravity.
  17. if (!IsOnFloor())
  18. velocity.Y -= gravity * (float)delta;
  19.  
  20. // Handle Jump.
  21. if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
  22. velocity.Y = JumpVelocity;
  23.  
  24.  
  25. Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
  26. Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
  27.  
  28. if (direction != Vector3.Zero)
  29. {
  30. GD.Print("Standing");
  31.  
  32. velocity.X = direction.X * Speed;
  33. velocity.Z = direction.Z * Speed;
  34. }
  35. else
  36. {
  37. GD.Print("Moving");
  38. velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
  39. velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed);
  40. }
  41.  
  42. Velocity = velocity;
  43. MoveAndSlide();
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment