- Home /
Multiplayer updates are really slow
This is hard to explain, and will seem like block text at first, and for that I apologize. I'm working on a multiplayer shooter.
All of the movement and turning in-game are really slow to the other players in the game, but they are responsive to the player moving, i.e. if I turn, it is really slow and has a lot of drag if you're looking at it, but on the client-side screen it's a normal speed. When a projectile is fired, and the firer is currently rotating, it will fire one of two ways:
A) If it is the LAN host firing the bullet, it will go normally, but other players will see it as firing from a location way before the host even turns to that place.
B) If it is a joining player, they will fire bullets, but they will fire in the direction that they just rotated from, usually about a second before. However, for the host, it looks completely normal.
I have all of the rotation, firing, and movement handled in one huge master file, in C#, so I'm just going to put the whole thing below. Any help is appreciated.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI;
public class PlayerController : NetworkBehaviour {
public float speed = 2.0f;
public float jumpHeight = 5.0f;
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotY = 0.0f;
private float rotX = 0.0f;
public float bulletCooldown = 0.5f;
private float bulletTime = 2.0f;
private float bulletDoubleTime = 0.0f;
public int bulletSpeed = 20;
public GameObject bulletPrefab;
public Transform bulletSpawn;
public Transform secondBulletSpawn;
public const int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
public RectTransform healthBar;
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.visible = false;
bulletDoubleTime = (Time.time / 2) + bulletCooldown;
}
void Update()
{
if (!isLocalPlayer)
return;
float fire = Input.GetAxis("Fire1");
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
float y = Input.GetAxis("Jump");
x = x * Time.deltaTime * speed;
z = z * Time.deltaTime * speed;
y = y * Time.deltaTime * jumpHeight;
transform.Translate(x, y, z);
//rotation
float mouseX = Input.GetAxis("MouseX");
float mouseY = -Input.GetAxis("MouseY");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
//firing
if (fire > 0 && Time.time > bulletTime)
{
CmdFire();
bulletTime = Time.time + bulletCooldown;
return;
}
if (fire > 0 && Time.time > bulletDoubleTime)
{
CmdDoubleFire();
bulletDoubleTime = Time.time + bulletCooldown;
return;
}
}
void OnChangeHealth(int currentHealth)
{
healthBar.sizeDelta = new Vector2(currentHealth, healthBar.sizeDelta.y);
}
[Command]
void CmdFire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletSpeed;
// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
[Command]
void CmdDoubleFire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
secondBulletSpawn.position,
secondBulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletSpeed;
// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
public override void OnStartLocalPlayer()
{
GetComponent<MeshRenderer>().material.color = Color.red;
}
public void TakeDamage(int amount)
{
if (!isServer)
{
return;
}
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
Debug.Log("Dead!");
}
}
}
Your answer
Follow this Question
Related Questions
How to Debug in a standalone build? 4 Answers
Access a variable across all other players (online multiplayer). 1 Answer
Can you make a FPS multiplayer on a Android or iOS device? 1 Answer
Multilevel shooter? 2 Answers
Making my fps a multiplayer 0 Answers