Facebook
From Ahmet, 9 Months ago, written in C#.
Embed
Download Paste or View Raw
Hits: 321
  1. using UnityEngine;
  2. using Unity.Netcode;
  3.  
  4. public class PlayerController : NetworkBehaviour
  5. {
  6.     [SerializeField] private float moveSpeed = 5f;
  7.     [SerializeField] private float sprintMultiplier = 2f;
  8.     [SerializeField] private float rotationSpeed = 100f;
  9.     [SerializeField] private float doubleTapTimeThreshold = 0.3f;
  10.     [SerializeField] private Animator animator;
  11.  
  12.     private Rigidbody rb;
  13.     private void Start()
  14.     {
  15.         if (!IsOwner && !IsServer) return;
  16.         this.rb = this.GetComponent<Rigidbody>();
  17.     }
  18.     private void FixedUpdate()
  19.     {
  20.         if (!IsOwner) return;
  21.  
  22.         Debug.Log("asd from " + gameObject.name);
  23.  
  24.         if (IsOwnedByServer)
  25.         {
  26.             this.HandleMovement();
  27.             this.HandleRotation();
  28.         }
  29.         else
  30.         {
  31.             this.SendPositionDataServerRPC();
  32.             this.SendRotationDataServerRPC();
  33.         }
  34.     }
  35.     private void HandleMovement()
  36.     {
  37.         if (!IsOwner) return;
  38.         float moveX = Input.GetAxisRaw("Horizontal");
  39.         float moveZ = Input.GetAxisRaw("Vertical");
  40.         bool sprintInput = Input.GetKey(KeyCode.LeftShift);
  41.  
  42.         float currentMoveSpeed = moveSpeed;
  43.         if (sprintInput)
  44.         {
  45.             currentMoveSpeed *= sprintMultiplier;
  46.             animator.SetBool("isSprinting", true);
  47.         }
  48.         else
  49.             animator.SetBool("isSprinting", false);
  50.  
  51.         Vector3 movement = new Vector3(moveX, 0f, moveZ).normalized * currentMoveSpeed;
  52.         if (movement != Vector3.zero)
  53.         {
  54.             this.rb.velocity = new Vector3(movement.x, this.rb.velocity.y, movement.z);
  55.             animator.SetBool("isWalking", true);
  56.         }
  57.         else
  58.         {
  59.             // Set velocity to zero to stop the Rigidbody
  60.             this.rb.velocity = Vector3.zero;
  61.             animator.SetBool("isWalking", false);
  62.         }
  63.  
  64.     }
  65.     private void HandleRotation()
  66.     {
  67.         if (!IsOwner) return;
  68.         Vector3 movementDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
  69.  
  70.         if (movementDirection != Vector3.zero)
  71.         {
  72.             Quaternion toRotation = Quaternion.LookRotation(movementDirection * rotationSpeed);
  73.             this.rb.MoveRotation(Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.fixedDeltaTime));
  74.         }
  75.     }
  76.  
  77.     [ServerRpc]
  78.     public void SendPositionDataServerRPC()
  79.     {
  80.         float moveX = Input.GetAxisRaw("Horizontal");
  81.         float moveZ = Input.GetAxisRaw("Vertical");
  82.         bool sprintInput = Input.GetKey(KeyCode.LeftShift);
  83.  
  84.         float currentMoveSpeed = moveSpeed;
  85.         if (sprintInput)
  86.         {
  87.             currentMoveSpeed *= sprintMultiplier;
  88.             animator.SetBool("isSprinting", true);
  89.         }
  90.         else
  91.             animator.SetBool("isSprinting", false);
  92.  
  93.         Vector3 movement = new Vector3(moveX, 0f, moveZ).normalized * currentMoveSpeed;
  94.         if (movement != Vector3.zero)
  95.         {
  96.             rb.velocity = new Vector3(movement.x, rb.velocity.y, movement.z);
  97.             animator.SetBool("isWalking", true);
  98.         }
  99.         else
  100.         {
  101.             // Set velocity to zero to stop the Rigidbody
  102.             rb.velocity = Vector3.zero;
  103.             animator.SetBool("isWalking", false);
  104.         }
  105.     }
  106.     [ServerRpc]
  107.     public void SendRotationDataServerRPC()
  108.     {
  109.         Vector3 movementDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
  110.  
  111.         if (movementDirection != Vector3.zero)
  112.         {
  113.             Quaternion toRotation = Quaternion.LookRotation(movementDirection * rotationSpeed);
  114.             rb.MoveRotation(Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.fixedDeltaTime));
  115.         }
  116.     }
  117. }