Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System;
- public partial class Player: CharacterBody3D
- {
- [Export] public float Speed = 10.0f;
- [Export] public float JumpVelocity = 4.5f;
- [Export] public float Gravity = 9.8f;
- private Vector3 _moveVelocity = Vector3.Zero;
- private Vector3 _pushVelocity = Vector3.Zero;
- private float dragCoefficient = 0.9f;
- private float Sensitivity = 0.003f;
- private Camera3D _camera;
- private RayCast3D _raycast;
- public override void _EnterTree()
- {
- SetMultiplayerAuthority(int.Parse(Name));
- }
- public override void _Ready()
- {
- _camera = GetNode<Camera3D>("Camera3D");
- _raycast = _camera.GetNode<RayCast3D>("RayCast3D");
- if (IsMultiplayerAuthority())
- {
- _camera.Current = true;
- }
- else
- {
- _camera.Current = false;
- }
- Position = new Vector3(0f, 2f, 0f);
- }
- public override void _PhysicsProcess(double delta)
- {
- if (IsMultiplayerAuthority())
- {
- // movement
- Vector3 direction = Vector3.Zero;
- if (Input.IsActionPressed("ui_up"))
- direction -= Transform.Basis.Z;
- if (Input.IsActionPressed("ui_down"))
- direction += Transform.Basis.Z;
- if (Input.IsActionPressed("ui_left"))
- direction -= Transform.Basis.X;
- if (Input.IsActionPressed("ui_right"))
- direction += Transform.Basis.X;
- direction = direction.Normalized();
- Vector3 horizontalVelocity = direction * Speed;
- _moveVelocity.X = horizontalVelocity.X;
- _moveVelocity.Z = horizontalVelocity.Z;
- // gravity
- if (!IsOnFloor())
- {
- _moveVelocity.Y -= Gravity * (float)delta;
- _pushVelocity.Y -= Gravity * (float)delta;//
- } else
- {
- _moveVelocity.Y = 0;
- }
- // drag for push velocity
- if (_pushVelocity.Length() > 0f)
- {
- var speed = _pushVelocity.Length();
- var drag = dragCoefficient * speed * (float)delta;
- _pushVelocity -= _pushVelocity.Normalized() * drag;
- if (_pushVelocity.LengthSquared() < 1e-6f)
- _pushVelocity = Vector3.Zero;
- }
- Velocity = _moveVelocity + _pushVelocity;
- if (Input.IsActionJustPressed("attack"))
- {
- TryShoot();
- }
- }
- MoveAndSlide();
- }
- public override void _UnhandledInput(InputEvent @event)
- {
- if (!IsMultiplayerAuthority()) return;
- if (@event is InputEventMouseMotion)
- {
- InputEventMouseMotion eventSomething = (InputEventMouseMotion)@event;
- RotateY(-eventSomething.Relative.X * Sensitivity);
- }
- }
- private void TryShoot()
- {
- if (!_raycast.IsColliding()) return;
- Node collider = _raycast.GetCollider() as Node;
- if (collider == null) return;
- if (IsMultiplayerAuthority())
- {
- Vector3 rayDirection = _raycast.GlobalTransform.Basis * _raycast.TargetPosition.Normalized();
- Rpc(nameof(ApplyPushRequest), collider.GetPath(), rayDirection);
- }
- }
- [Rpc(MultiplayerApi.RpcMode.AnyPeer)]
- private void ApplyPushRequest(NodePath targetPath, Vector3 rayDirection)
- {
- Node target = GetNodeOrNull(targetPath);
- if (target is Player targetPlayer)
- {
- targetPlayer.ApplyPush(rayDirection);
- }
- }
- public void ApplyPush(Vector3 rayDirection)
- {
- if (!IsMultiplayerAuthority()) return;
- _pushVelocity = rayDirection * 5f + Vector3.Up * 15f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment