Arvis93

Godot multiplayer physics impulse test

Nov 13th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.04 KB | Gaming | 0 0
  1. using Godot;
  2. using System;
  3.  
  4. public partial class Player: CharacterBody3D
  5. {
  6.     [Export] public float Speed = 10.0f;
  7.     [Export] public float JumpVelocity = 4.5f;
  8.     [Export] public float Gravity = 9.8f;
  9.  
  10.  
  11.     private Vector3 _moveVelocity = Vector3.Zero;
  12.     private Vector3 _pushVelocity = Vector3.Zero;
  13.     private float dragCoefficient = 0.9f;
  14.  
  15.     private float Sensitivity = 0.003f;
  16.  
  17.     private Camera3D _camera;
  18.     private RayCast3D _raycast;
  19.  
  20.     public override void _EnterTree()
  21.     {
  22.         SetMultiplayerAuthority(int.Parse(Name));
  23.     }
  24.     public override void _Ready()
  25.     {
  26.         _camera = GetNode<Camera3D>("Camera3D");
  27.         _raycast = _camera.GetNode<RayCast3D>("RayCast3D");
  28.         if (IsMultiplayerAuthority())
  29.         {
  30.             _camera.Current = true;
  31.         }
  32.         else
  33.         {
  34.             _camera.Current = false;
  35.         }
  36.         Position = new Vector3(0f, 2f, 0f);
  37.     }
  38.  
  39.     public override void _PhysicsProcess(double delta)
  40.     {
  41.         if (IsMultiplayerAuthority())
  42.         {
  43.             // movement
  44.             Vector3 direction = Vector3.Zero;
  45.  
  46.             if (Input.IsActionPressed("ui_up"))
  47.                 direction -= Transform.Basis.Z;
  48.             if (Input.IsActionPressed("ui_down"))
  49.                 direction += Transform.Basis.Z;
  50.             if (Input.IsActionPressed("ui_left"))
  51.                 direction -= Transform.Basis.X;
  52.             if (Input.IsActionPressed("ui_right"))
  53.                 direction += Transform.Basis.X;
  54.             direction = direction.Normalized();
  55.  
  56.             Vector3 horizontalVelocity = direction * Speed;
  57.             _moveVelocity.X = horizontalVelocity.X;
  58.             _moveVelocity.Z = horizontalVelocity.Z;
  59.  
  60.             // gravity
  61.             if (!IsOnFloor())
  62.             {
  63.                 _moveVelocity.Y -= Gravity * (float)delta;
  64.                 _pushVelocity.Y -= Gravity * (float)delta;//
  65.             } else
  66.             {
  67.                 _moveVelocity.Y = 0;
  68.             }
  69.  
  70.             // drag for push velocity
  71.             if (_pushVelocity.Length() > 0f)
  72.             {
  73.                 var speed = _pushVelocity.Length();
  74.                 var drag = dragCoefficient * speed * (float)delta;
  75.                 _pushVelocity -= _pushVelocity.Normalized() * drag;
  76.  
  77.  
  78.                 if (_pushVelocity.LengthSquared() < 1e-6f)
  79.                     _pushVelocity = Vector3.Zero;
  80.             }
  81.  
  82.  
  83.             Velocity = _moveVelocity + _pushVelocity;
  84.  
  85.             if (Input.IsActionJustPressed("attack"))
  86.             {
  87.                 TryShoot();
  88.             }
  89.         }
  90.  
  91.  
  92.         MoveAndSlide();
  93.     }
  94.  
  95.     public override void _UnhandledInput(InputEvent @event)
  96.     {
  97.         if (!IsMultiplayerAuthority()) return;
  98.  
  99.         if (@event is InputEventMouseMotion)
  100.         {
  101.             InputEventMouseMotion eventSomething = (InputEventMouseMotion)@event;
  102.             RotateY(-eventSomething.Relative.X * Sensitivity);
  103.         }
  104.     }
  105.  
  106.  
  107.     private void TryShoot()
  108.     {
  109.         if (!_raycast.IsColliding()) return;
  110.  
  111.         Node collider = _raycast.GetCollider() as Node;
  112.  
  113.         if (collider == null) return;
  114.  
  115.         if (IsMultiplayerAuthority())
  116.         {
  117.             Vector3 rayDirection = _raycast.GlobalTransform.Basis * _raycast.TargetPosition.Normalized();
  118.             Rpc(nameof(ApplyPushRequest), collider.GetPath(), rayDirection);
  119.         }
  120.     }
  121.    
  122.  
  123.     [Rpc(MultiplayerApi.RpcMode.AnyPeer)]
  124.     private void ApplyPushRequest(NodePath targetPath, Vector3 rayDirection)
  125.     {
  126.         Node target = GetNodeOrNull(targetPath);
  127.         if (target is Player targetPlayer)
  128.         {
  129.             targetPlayer.ApplyPush(rayDirection);
  130.         }
  131.     }
  132.  
  133.  
  134.     public void ApplyPush(Vector3 rayDirection)
  135.     {
  136.         if (!IsMultiplayerAuthority()) return;
  137.         _pushVelocity = rayDirection * 5f + Vector3.Up * 15f;
  138.     }
  139.  
  140.  
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment