Basically I Have A Script That Moves The Players For Some Reason 2 Serverrpcs In The Script Dont Work Client Side (Added Debug Logs To Test) Tried Everything Nothing Worked So I Am Here So Maybe Someone Can Help Me! Here’s The Scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.Netcode;
public class PlayerMovementTutorial : NetworkBehaviour
{
[Header("Movement")]
public float moveSpeed;
public float groundDrag;
public float jumpForce;
public float jumpCooldown;
public float airMultiplier;
bool readyToJump;
[HideInInspector] public float walkSpeed;
[HideInInspector] public float sprintSpeed;
[Header("Keybinds")]
public KeyCode jumpKey = KeyCode.Space;
[Header("Ground Check")]
public float playerHeight;
public LayerMask whatIsGround;
bool grounded;
public Transform orientation;
public Camera cam;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
public Rigidbody rb;
private void Start()
{
if (!IsOwner)
{
orientation.gameObject.SetActive(false);
this.gameObject.GetComponentInChildren<CamPos>().gameObject.SetActive(false);
return;
}
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
readyToJump = true;
}
private void Update()
{
if (!IsOwner)
{
//this.enabled = false;
orientation.gameObject.SetActive(false);
this.gameObject.GetComponentInChildren<CamPos>().gameObject.SetActive(false);
return;
}
// ground check
if (Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround))
{
Debug.Log("Hey!");
grounded = true;
}
else grounded = false;
MovePlayerServerRpc();
MyInput();
SpeedControlServerRpc();
// handle drag
if (grounded)
rb.drag = groundDrag;
else
rb.drag = 0;
}
private void FixedUpdate()
{
}
private void MyInput()
{
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
Debug.Log("My Input");
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
Debug.Log(horizontalInput);
Debug.Log(verticalInput);
MovePlayerServerRpc();
// when to jump
if (Input.GetKey(jumpKey) && readyToJump && grounded)
{
Debug.Log("Jump");
readyToJump = false;
JumpServerRpc();
MovePlayerServerRpc();
Invoke(nameof(ResetJump), jumpCooldown);
}
}
[ServerRpc(RequireOwnership = false)]
private void MovePlayerServerRpc()
{
Debug.Log("Move");
// calculate movement direction
// on ground
if (grounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
Debug.Log("Move Grounded");
}
// in air
else if (!grounded)
rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
}
[ServerRpc]
private void SpeedControlServerRpc()
{
MovePlayerServerRpc();
Debug.Log("SpeedControl");
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
// limit velocity if needed
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
Debug.Log("Cool Calc");
}
}
[ServerRpc]
private void JumpServerRpc()
{
Debug.Log("JumpyJump");
// reset y velocity
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
private void ResetJump()
{
readyToJump = true;
}
}