Hi im trying to setup 4 directional shooting for my characters in a multiplayer game
but only the host can shoot?
where am i going wrong
Main movement script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerMovement : NetworkBehaviour {
public Vector2 speed = new Vector2(1, 1);
public GameObject Playercamera;
private Animator anim;
public int Direction;
public override void OnStartLocalPlayer()
{
Playercamera.SetActive(true);
anim = transform.FindChild("Character").GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
return;
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// Prepare the movement for each direction
Vector2 movement = new Vector3(speed.x * inputX, speed.y * inputY, 0);
movement *= Time.deltaTime;// Makes the movement relative to time
transform.Translate(movement);// Moves the object
DirectionCheck();
}
void DirectionCheck()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
//Taking the input And figuring out what direction were facing
if (inputX > 0.2 || inputX < -0.2 || inputY > 0.2 || inputY < -0.2)
{
anim.SetBool("Walking", true);
if (inputX > 0)
{
Direction = 1;
anim.SetFloat("InputX", 1f);
anim.SetFloat("InputY", 0f);
}
else if (inputX < 0)
{
Direction = 2;
anim.SetFloat("InputX", -1f);
anim.SetFloat("InputY", 0f);
}
else if (inputY > 0)
{
Direction = 3;
anim.SetFloat("InputY", 1f);
anim.SetFloat("InputX", 0f);
}
else if (inputY < 0)
{
Direction = 4;
anim.SetFloat("InputY", -1f);
anim.SetFloat("InputX", 0f);
}
}
else
{
anim.SetBool("Walking", false);
}
}
}
and this is how im doing the shooting
it appears the direction variable i have does not seem to send or do anything on players other than the host
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Shoot : NetworkBehaviour
{
public GameObject bulletPrefab;
public GameObject ProjectileSpawnPointT;
public GameObject ProjectileSpawnPointB;
public GameObject ProjectileSpawnPointL;
public GameObject ProjectileSpawnPointR;
public PlayerMovement pm;
// Use this for initialization
void Start () {
}
public override void OnStartLocalPlayer()
{
//pm = GetComponent<PlayerMovement>();
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer){
return;
}
if (Input.GetKeyDown(KeyCode.Space))
{
CmdFire();
}
}
[Command]
void CmdFire()
{
if(pm.Direction == 1)
{
var pos = ProjectileSpawnPointR.transform.position;
var bullet = (GameObject)Instantiate(bulletPrefab, pos, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().AddForce(transform.right * 200);
NetworkServer.Spawn(bullet);
Destroy(bullet, 2.0f);
}
else if (pm.Direction == 2)
{
var pos = ProjectileSpawnPointL.transform.position;
var bullet = (GameObject)Instantiate(bulletPrefab, pos, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().AddForce(-transform.right * 200);
NetworkServer.Spawn(bullet);
Destroy(bullet, 2.0f);
}
else if (pm.Direction == 3)
{
var pos = ProjectileSpawnPointT.transform.position;
var bullet = (GameObject)Instantiate(bulletPrefab, pos, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().AddForce(transform.up * 200);
NetworkServer.Spawn(bullet);
Destroy(bullet, 2.0f);
}
else if (pm.Direction == 4)
{
var pos = ProjectileSpawnPointB.transform.position;
var bullet = (GameObject)Instantiate(bulletPrefab, pos, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().AddForce(-transform.up * 200);
NetworkServer.Spawn(bullet);
Destroy(bullet, 2.0f);
}
}
}