the bullets in not syncing in clients. it works as expected if played on host. even hit effects. but the bullet does not display its position on clients…
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Player_Move : NetworkBehaviour {
[SyncVar]
public int ammo = 999;
AudioSource audioSource;
public Transform barrelend;
public GameObject projectile;
private bool canshoot;
//bullet variables
public float shotCooldown = 0.1f;
float elapsetime;
public float bulletspeed = 9000f;
private bool shootready;
//jumping
Collider[] groundCollisions;
public float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
private Transform groundcheck;
BitStream stream;
private Quaternion lookleft;
private Quaternion lookright;
private Animator anim;
public CJ cj;
public float runspeed = 5f;
public float jumpheight = 100f;
// private bool facingRight;
private Rigidbody myRB;
private bool grounded, falling;
RaycastHit hit;
public float rayrange= 1f;
Vector3 movement;
public float mSpeed = 4f;
public float rayDist =1f;
public float jumpspeed = 200f;
void Awake()
{
cj = GameObject.FindGameObjectWithTag ("controller").GetComponentInChildren<CJ> ();
groundcheck = gameObject.transform.Find ("groundcheckob");
audioSource = gameObject.GetComponent<AudioSource> ();
// facingRight = true;
// lookright = transform.rotation;
// lookleft = lookright * Quaternion.Euler (0, 180, 0);
}
// Use this for initialization ...///
void Start () {
anim = gameObject.GetComponent<Animator> ();
myRB = gameObject.GetComponent<Rigidbody> ();
if (isLocalPlayer) {
Camera.main.GetComponent<Camera_Follow> ().target = transform;
}
anim = gameObject.GetComponent<Animator> ();
if (isLocalPlayer)
canshoot = true;
}
// Update is called once per frame...........................................................////..............................
void FixedUpdate () {
if (!isLocalPlayer) {
return;
}
// float h = Input.GetAxis ("Horizontal");
//float v = Input.GetAxis ("Vertical");
float h = cj.Horizontal ();
float v = cj.Vertical();
//Vector3 movement = new Vector3 (h, 0.0f, v);
move (h,v);
jump ();
if (h != 0) {
transform.rotation = Quaternion.LookRotation (Vector3.forward* h);
}
//
// if (h > 0 && !facingRight) {
// flip ();
//
// } else if (h < 0 && facingRight) {
// flip ();
// if (h < 0) {
// transform.rotation = lookleft;
// } else {
// transform.rotation = lookright;
// }
//
groundCollisions = Physics.OverlapSphere (groundcheck.position, groundCheckRadius, groundLayer);
if (groundCollisions.Length > 0) {
grounded = true;
anim.SetBool ("jump", false);
} else {
grounded = false;
anim.SetBool ("jump", true);
}
//jumping
if (grounded && Input.GetAxis ("Jump") > 0) {
grounded = false;
myRB.AddForce (new Vector3 (0, jumpheight, 0));
}
//Vector3 dir = transform.forward.normalized;
//transform.forward = dir;
// transform.Translate (Vector3.forward * h * Time.deltaTime);
}
void Update()
{
elapsetime += Time.deltaTime;
if (canshoot) {
if (Input.GetButtonDown ("Fire1") && elapsetime > shotCooldown) {
elapsetime = 0f;
CmdFireShot ();
} else {
anim.SetBool ("Shoot", false);
}
}
}
// update function here/// <summary>
/// M///////////....................................................................................................//
/////////////////////////////movment of the player
void move(float h, float v)
{
//movement.Set (0,0, h);
//movement = movement.normalized * mSpeed * Time.deltaTime;
//myRB.MovePosition (transform.position + movement);
myRB.velocity = new Vector3 (0, myRB.velocity.y,h * runspeed);
anim.SetFloat ("Speed", h);
anim.SetFloat ("Aim", v );
//
// if (v != 0) {
// anim.SetLayerWeight (1, 20 * Time.deltaTime);
// } else {
// anim.SetLayerWeight (1, 0 );
// }
}
//.............//jump function...........///
void jump()
{
if (Input.GetButtonDown ("Jump") && grounded==true) {
grounded = false;
//myRB.AddForce (Vector3.up * jumpspeed);
myRB.AddForce (new Vector3 (0, jumpheight, 0));
}
}
///.........................../////checks ground below the player feet.....//
//ground check
//
// void flip()
// {
// facingRight = !facingRight;
// //Vector3 theScale = transform.localScale;
// //theScale.z *= -1;
//
// transform.Rotate (0, -180, 0);
//
// // transform.localScale = theScale;
// }
// public override void OnStartLocalPlayer()
// {
//
// GetComponent<NetworkAnimator> ().SetParameterAutoSend (0,true);
// GetComponent<NetworkAnimator> ().SetParameterAutoSend(1,true);
//
// }
//
// public override void PreStartClient()
// {
// GetComponent<NetworkAnimator> ().SetParameterAutoSend (0,true);
// GetComponent<NetworkAnimator> ().SetParameterAutoSend(1,true);
//
// }
public override void OnStartLocalPlayer()
{
Text txt = GameObject.FindWithTag ("TextScreen").GetComponent<Text> ();;
txt.text = "";
}
[Command]
void CmdFireShot()
{
if (ammo > 0) {
anim.SetBool ("Shoot", true);
ammo -= 1;
// var bullet = (GameObject)Instantiate (projectile, barrelend.position, barrelend.rotation);
//
// bullet.GetComponent<Rigidbody> ().AddForce (bullet.transform.forward * bulletspeed);
// NetworkServer.Spawn (bullet);
var bullet = (GameObject)Instantiate (projectile, barrelend.transform.position, barrelend.transform.rotation);
// bullet.GetComponent<Rigidbody> ().velocity = bullet.transform.forward * bulletspeed;
bullet.GetComponent<Rigidbody> ().AddForce (bullet.transform.forward * bulletspeed);
NetworkServer.Spawn (bullet.gameObject);
audioSource.Play ();
Destroy (bullet, 3f);
} else {
anim.SetBool ("Shoot", false);
}
}
}
nobody answers in answers
http://answers.unity3d.com/questions/1331269/my-network-spawn-bullet-flys-off-in-server-but-dro.html