У меня в скрипте есть две функции.
Код: Выделить всё
[Command]
private void CmdPunch()
{
// Cast a sphere in the forward direction of the player
RaycastHit hit;
float sphereRadius = 0.5f; // Adjust the radius as needed
if (Physics.SphereCast(transform.position, sphereRadius, steve.forward, out hit, 6))
{
// Check if the object hit is on the "Player" layer
if (hit.collider.CompareTag("Player"))
{
// Get the parent GameObject of the hit collider
GameObject hitParent = hit.collider.transform.parent.gameObject;
// Get the NetworkIdentity of the hit parent GameObject
NetworkIdentity hitNetworkIdentity = hitParent.GetComponent();
if (hitNetworkIdentity != null)
{
// Get the NetworkConnectionToClient from the hit NetworkIdentity
NetworkConnectionToClient hitConnection = hitNetworkIdentity.connectionToClient;
// Now you can use hitConnection to perform further actions, such as reducing the player's speed
if (hitConnection != null)
{
// Call TargetPunch on the hit player
TargetPunch(hitConnection);
}
}
}
}
}
[TargetRpc]
public void TargetPunch(NetworkConnectionToClient target)
{
Debug.Log($"Target hit");
canJump = false;
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... rror-unity
Мобильная версия