S
S
Senture2018-03-29 23:25:04
Unity
Senture, 2018-03-29 23:25:04

How to fix "Trying to send command for object without authority." in multiplayer?

Hello!
There are 2 classes, Bullet and PlayerShoot:
PlayerShoo (the script weighs on the player): t:

using UnityEngine;
using UnityEngine.Networking;

public class PlayerShoot : NetworkBehaviour
{
    public Weapon weapon;

    [SerializeField]
    private Transform bullet; // префаб пули
    [SerializeField]
    private Transform pivot; // пустой оъбект для спавна пули по координатам данного объекта

    public int speed = 600; // скорость пули
    public float damage = 10f; // урон пули

  void Start ()
    {
        speed = 600;
        damage = 10f;
        if (cam == null)
        {
            Debug.LogError("No camera");
            this.enabled = false;
        }
  }
  

  void Update ()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
     }

    [Client]
    void Shoot()
    {
        Instantiate(bullet, pivot.position, pivot.rotation); // Спавн пули
    }

    [Command]
    void CmdPlayerShoot(string _ID, float damage)
    {
        Debug.Log(_ID + " поврежден!");

        Player player = GameManager.GetPlayer(_ID);
        player.TakeDamage(damage);
    }
}

Bullet (the script weighs on the bullet):
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Bullet : MonoBehaviour
{

    private Rigidbody rb;

    private Vector3 lastPos;

    public GameObject goPlayer;

    [SerializeField]
    private LayerMask mask;

    PlayerShoot playerShoot;

    private int speed;
    private float damage;
    // Задаем настройки для пули
    void Start ()
    {
        rb = GetComponent<Rigidbody>();

        playerShoot = goPlayer.GetComponent<PlayerShoot>();
        int courrentSpeed = playerShoot.speed;
        speed = courrentSpeed;
        damage = playerShoot.damage;

        lastPos = transform.position;
    }

    // Запускаем пулю вперед и если она поппадает в объект с тегом "Player" снимаем hp у игрока в которого попали
    void Update ()
    {
        rb.transform.Translate(Vector3.right * speed * Time.deltaTime);

        RaycastHit _hit;
        if (Physics.Linecast(lastPos, transform.position, out _hit, mask))
        {
            Destroy(gameObject);
            if (_hit.collider.tag == "Player")
            {
                //playerShoot.ReadShoot(_hit);
                goPlayer.GetComponent<PlayerShoot>().ReadShoot(_hit); // Вот тут ошибка: см. ниже
            }
        }

        lastPos = transform.position;
    }
}

error: Trying to send command for object without authority.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
PlayerShoot:CallCmdPlayerShoot(String, Single)
PlayerShoot:ReadShoot(RaycastHit) (at Assets/Scripts/PlayerShoot.cs:68)
Bullet:Update() (at Assets/Scripts /Bullet.cs:67)
How to fix? Help me please.
PS Thank you very much!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question