jzak

Untitled

Apr 28th, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. using Godot;
  2. using System;
  3.  
  4. public partial class Player : CharacterBody3D
  5. {
  6. public Vector3 LeanPosition = new Vector3(1.0f, 0.0f,0.0f);
  7. public const float Speed = 5.0f;
  8. public const float JumpVelocity = 4.5f;
  9. public Vector3 restart = new Vector3(1.5f,0.0f,0.0f);
  10. public float t = 0.0f;
  11. public bool HasLeaned = false;
  12. Node3D neck;
  13. Camera3D camera;
  14. RayCast3D RayCast3D2;
  15. public bool HasLeanedLeft = false;
  16. public bool HasLeanedRight = false;
  17. Vector3 LeanAmount;
  18. Vector3 CollisionTarget;
  19. float RotationTarget;
  20. StaticBody3D LeanShape;
  21. Vector3 initialPosition;
  22. public override void _Ready()
  23. {
  24.  
  25. neck = GetNode<Node3D>("neck");
  26. camera = GetNode<Camera3D>("neck/Camera3D");
  27. RayCast3D2 = GetNode<RayCast3D>("neck/RayCast3D2");
  28. LeanShape = GetNode<StaticBody3D>("StaticBody3D");
  29. initialPosition = LeanShape.Position;
  30.  
  31. }
  32. public override void _UnhandledInput(InputEvent @event){
  33.  
  34. if(@event is InputEventMouseButton){
  35. Input.MouseMode = Input.MouseModeEnum.Captured;
  36. }
  37. else if(Input.IsActionPressed("ui_cancel")){
  38.  
  39. Input.MouseMode = Input.MouseModeEnum.Visible;
  40. }
  41. if(@event is InputEventMouseMotion mouseMotion){
  42. // Modify the rotation directly instead of using RotateX and RotateY
  43. neck.RotateY(-mouseMotion.Relative.X * 0.01f);
  44. camera.RotateX(-mouseMotion.Relative.Y * 0.01f);
  45. Vector3 newRotation = camera.Rotation;
  46. newRotation.X = Mathf.Clamp(newRotation.X, Mathf.DegToRad(-30), Mathf.DegToRad(60));
  47. camera.Rotation = newRotation;
  48. }
  49.  
  50.  
  51. if(Input.IsActionJustPressed("lean_right" )){
  52. HasLeanedRight = true;
  53. LeanAmount = neck.Transform.Basis.X;
  54. CollisionTarget = new Vector3(0.0f, 1.0f, neck.Transform.Basis.X.Z);
  55. RotationTarget = Mathf.DegToRad(-15.0f);
  56. }
  57. else if(Input.IsActionJustReleased("lean_right")){
  58. CollisionTarget = new Vector3(0,0,1);
  59.  
  60. HasLeanedRight = false;
  61. LeanAmount = Vector3.Zero;
  62. CollisionTarget = new Vector3(0.0f,1.0f,0.0f);
  63. RotationTarget = Mathf.DegToRad(0.0f);
  64.  
  65. }
  66. if(Input.IsActionJustPressed("lean_left" ) ){
  67. HasLeanedLeft = true;
  68.  
  69. LeanAmount = -neck.Transform.Basis.X;
  70. CollisionTarget = -neck.Transform.Basis.X;
  71.  
  72. RotationTarget = Mathf.DegToRad(15.0f);
  73.  
  74. }
  75. else if(Input.IsActionJustReleased("lean_left")){
  76. HasLeanedLeft = false;
  77. LeanAmount = Vector3.Zero;
  78. CollisionTarget = new Vector3(0.0f,1.0f,0.0f);
  79. RotationTarget = Mathf.DegToRad(0.0f);
  80.  
  81. }
  82.  
  83.  
  84.  
  85. }
  86. public override void _PhysicsProcess(double delta)
  87. {
  88. Vector3 velocity = Velocity;
  89. float weight = 1f - Mathf.Exp(-6.0f * (float)delta);
  90.  
  91. // Add the gravity.
  92. if (!IsOnFloor())
  93. {
  94. velocity += GetGravity() * (float)delta;
  95. }
  96.  
  97. // Handle Jump.
  98. if (Input.IsActionJustPressed("jump") && IsOnFloor()){
  99. velocity.Y = JumpVelocity;
  100. }
  101.  
  102. // Get the input direction and handle the movement/deceleration.
  103. // As good practice, you should replace UI actions with custom gameplay actions.
  104. Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
  105. Vector3 direction = (neck.Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
  106. if (direction != Vector3.Zero){
  107. velocity.X = direction.X * Speed;
  108. velocity.Z = direction.Z * Speed;
  109. }
  110. else{
  111. velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
  112. velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed);
  113. }
  114. if (Input.IsActionJustPressed("move_forward") && IsOnFloor()){
  115. direction.X ++;
  116. }
  117. camera.Rotation = new Vector3(camera.Rotation.X,camera.Rotation.Y, Mathf.LerpAngle(camera.Rotation.Z, RotationTarget, weight));
  118. neck.Position = neck.Position.Lerp(LeanAmount, weight);
  119. LeanShape.Position = LeanShape.Position.Lerp(CollisionTarget, weight);
  120. Velocity = velocity;
  121. GD.Print(LeanShape.Position);
  122. MoveAndSlide();
  123. }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment