Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mcdemrer · Jul 26, 2017 at 02:05 PM · multiplayerfpsmultipleslowshooter

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!");
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges