Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System;
- public partial class Player : CharacterBody3D
- {
- public Vector3 LeanPosition = new Vector3(1.0f, 0.0f,0.0f);
- public const float Speed = 5.0f;
- public const float JumpVelocity = 4.5f;
- public Vector3 restart = new Vector3(1.5f,0.0f,0.0f);
- public float t = 0.0f;
- public bool HasLeaned = false;
- Node3D neck;
- Camera3D camera;
- RayCast3D RayCast3D2;
- public bool HasLeanedLeft = false;
- public bool HasLeanedRight = false;
- Vector3 LeanAmount;
- Vector3 CollisionTarget;
- float RotationTarget;
- StaticBody3D LeanShape;
- Vector3 initialPosition;
- public override void _Ready()
- {
- neck = GetNode<Node3D>("neck");
- camera = GetNode<Camera3D>("neck/Camera3D");
- RayCast3D2 = GetNode<RayCast3D>("neck/RayCast3D2");
- LeanShape = GetNode<StaticBody3D>("StaticBody3D");
- initialPosition = LeanShape.Position;
- }
- public override void _UnhandledInput(InputEvent @event){
- if(@event is InputEventMouseButton){
- Input.MouseMode = Input.MouseModeEnum.Captured;
- }
- else if(Input.IsActionPressed("ui_cancel")){
- Input.MouseMode = Input.MouseModeEnum.Visible;
- }
- if(@event is InputEventMouseMotion mouseMotion){
- // Modify the rotation directly instead of using RotateX and RotateY
- neck.RotateY(-mouseMotion.Relative.X * 0.01f);
- camera.RotateX(-mouseMotion.Relative.Y * 0.01f);
- Vector3 newRotation = camera.Rotation;
- newRotation.X = Mathf.Clamp(newRotation.X, Mathf.DegToRad(-30), Mathf.DegToRad(60));
- camera.Rotation = newRotation;
- }
- if(Input.IsActionJustPressed("lean_right" )){
- HasLeanedRight = true;
- LeanAmount = neck.Transform.Basis.X;
- CollisionTarget = new Vector3(0.0f, 1.0f, neck.Transform.Basis.X.Z);
- RotationTarget = Mathf.DegToRad(-15.0f);
- }
- else if(Input.IsActionJustReleased("lean_right")){
- CollisionTarget = new Vector3(0,0,1);
- HasLeanedRight = false;
- LeanAmount = Vector3.Zero;
- CollisionTarget = new Vector3(0.0f,1.0f,0.0f);
- RotationTarget = Mathf.DegToRad(0.0f);
- }
- if(Input.IsActionJustPressed("lean_left" ) ){
- HasLeanedLeft = true;
- LeanAmount = -neck.Transform.Basis.X;
- CollisionTarget = -neck.Transform.Basis.X;
- RotationTarget = Mathf.DegToRad(15.0f);
- }
- else if(Input.IsActionJustReleased("lean_left")){
- HasLeanedLeft = false;
- LeanAmount = Vector3.Zero;
- CollisionTarget = new Vector3(0.0f,1.0f,0.0f);
- RotationTarget = Mathf.DegToRad(0.0f);
- }
- }
- public override void _PhysicsProcess(double delta)
- {
- Vector3 velocity = Velocity;
- float weight = 1f - Mathf.Exp(-6.0f * (float)delta);
- // Add the gravity.
- if (!IsOnFloor())
- {
- velocity += GetGravity() * (float)delta;
- }
- // Handle Jump.
- if (Input.IsActionJustPressed("jump") && IsOnFloor()){
- velocity.Y = JumpVelocity;
- }
- // Get the input direction and handle the movement/deceleration.
- // As good practice, you should replace UI actions with custom gameplay actions.
- Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
- Vector3 direction = (neck.Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
- if (direction != Vector3.Zero){
- velocity.X = direction.X * Speed;
- velocity.Z = direction.Z * Speed;
- }
- else{
- velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
- velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed);
- }
- if (Input.IsActionJustPressed("move_forward") && IsOnFloor()){
- direction.X ++;
- }
- camera.Rotation = new Vector3(camera.Rotation.X,camera.Rotation.Y, Mathf.LerpAngle(camera.Rotation.Z, RotationTarget, weight));
- neck.Position = neck.Position.Lerp(LeanAmount, weight);
- LeanShape.Position = LeanShape.Position.Lerp(CollisionTarget, weight);
- Velocity = velocity;
- GD.Print(LeanShape.Position);
- MoveAndSlide();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment