It is my first unity project and i dont know much about it. I was trying to make a grab and throw system but it doesn’t work. I manage to make it work with the host player but with a none-host client the object isn’t tracked by the server.
I have a player who move and works fine and he has and holdPoint in front of him to hold objects.
My player use this script for grab and throw but when i try with a client it says that my client doesnt have the authority to do a command. I have not set anything about authority in the objects that i use and i’m not sure why this happen. Hopefully you guys can help.
public class grabScript :NetworkBehaviour {
public bool grabbed;
public float distance=1f;
RaycastHit2D hit;
public Transform holdPoint;
public float throwForce;
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
{
return;
}
CmdGrabAndThrow();
}
[Command]
void CmdGrabAndThrow()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (!grabbed)
{
Physics2D.queriesStartInColliders = false;
hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y - 0.7f), Vector2.right * transform.localScale.x, distance);
if (hit.collider != null)
{
grabbed = true;
}
}
else
{
grabbed = false;
hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x, 1);
}
}
if (grabbed)
{
if (Input.GetKeyDown(KeyCode.L))
{
grabbed = false;
hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(transform.localScale.x * throwForce, 1);
}
else
{
hit.collider.gameObject.transform.position = holdPoint.position;
}
}
}
}