- Home /
how do you send commands from objects that dont have "Authortiy" unet
I made a rocket launcher that uses projectiles rather than raycast since raycast is instant etc
but when I try to send values of damage to the main player again it always gives me the warning doesnt have authority heres idk if their is a correct way of doing this and if so let me know
// on player object with authority
public void SendDamge(string name, float Splashdamage) {
CmdPlayerShot(name, Splashdamage);
}
[Command]
public void CmdPlayerShot(string _playerID, float _damage)
{
UnityEngine.Debug.Log(_playerID + " has been shot");
Player _player = GameManager1.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
}
// on rocket without authority projectile that is instantiated from the player
public class RocketProj : Rocket
{
public bool hitPlayer;
private Vector3 midScreen;
private void OnCollisionEnter(Collision collision)
{
Debug.Log("test");
var tempexpEffect = Instantiate(explosionEffect, this.transform.position, this.transform.rotation);
Destroy(tempexpEffect, 1);
if (collision.collider.tag == "Player")
{
hitPlayer = true;
}
else hitPlayer = false;
//Destroy(this.gameObject);
}
public void hitPlayerCalcSplash(RaycastHit _hitInfo)
{
if (hitPlayer == true) {
Debug.Log(_hitInfo.collider.name);
hitColliders = Physics.OverlapSphere(_hitInfo.point, splashRadius);
int i = 0;
while (i < hitColliders.Length)
{
if (hitColliders[i].tag == "Player")
{
proximity = (_hitInfo.point - _hitInfo.collider.transform.position).magnitude;
Splashdamage = CritHitDamage - (proximity / splashRadius);
UnityEngine.Debug.Log("Rocket Splash hit a player");
// SendDamge(hitColliders[i].name, Splashdamage);
Destroy(this.gameObject);
}
} i++;
}
}
}
And do i have to spawn the rocket thru unets network spawn to show the rocket for everyplayer connected to the server including host and client?
Yes. But I think there are other ways also. This is detailed in the manual I do believe.
Any idea on the main issue at the top tho ^
Answer by Bunny83 · Apr 02, 2018 at 11:07 PM
You can't send commands to objects you don't have authority over. Your main communication object is your player object. So the usual eventflow is:
Your client receives user input
You send a command through your playerobject to the server
On the serverside of your playerobject the server may perform any server-side actions like spawning objects or sending client RPCs back to the clients.
You have to design your playerobject and your command methods in a way so you can communicate whatever the client may want to affect on the server / game. Most of the logic should happen on the serverside. So a client may just send a command to the server to launch a rocket. The server will instantiate and spawn the rocket. Any hit-detection of the rocket will happen on the server only. The rocket is usually owned by the server. On hit the server will decide about health reduction and any other actions that follow the impact. Of course the rocket need to have a network identity like any prefab that should be spawned for all peers.