So, everything is fine, the host and clients rotate perfectly for everyone, the host fires and the proyectile takes the desired direction, which is the host’s facing direction, but the clients no matter what, when fire, the proyectile always move directly up. by its code I assume it is not spawned rotated, I dont really know what might this be.
My 2D game player has the following code to fire:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Movement : NetworkBehaviour {
private float fireRate; //A number
public GameObject shtPrefab; //The proyectile prefab (abreviation from shootPrefab)
[SyncVar]private Quaternion rot; //A quaternion
void Update () {
if (!isLocalPlayer) { return; } //You know what this one is for
rot = transform.rotation; //Sync Var rot is set to the current rotation
if (Input.GetKeyDown(KeyCode.Space)) { CmdFire(); } //If you press spacebar you fire
}
[Command] void CmdFire()
{
if (Time.time >= fireRate)
{
GameObject proyectile = (GameObject)Instantiate(shtPrefab, transform.position, rot); //Instantiates the proyectile shtPrefab at current position and, rotation, as rot is supposed to be the current rotation. Is here the error?
fireRate = Time.time + 0.5f; //Makes you unable to fire as fast as you can click, only every half a sec.
NetworkServer.Spawn(projectile); //Spawns the proyectile to everyone
Physics2D.IgnoreCollision(projectile.GetComponent<CircleCollider2D>(), GetComponent<BoxCollider2D>()); // Ignores the collision with the proyectile
}
}
}
and the shtPrefab contains the following script:
void Update()
{
GetComponent<Rigidbody2D>().velocity = transform.up * MoveSpeed;
GetComponent<Rigidbody2D>().angularVelocity = 0;
}
Here is a picture of the player’s and shoot’s components:
Player
Shoot
I am kinda new to multiplayer, so If its a really obvious mistake, sorry xd.
Thank you for any help :3