Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 2K9CON · Jan 08, 2014 at 07:24 PM · multiplayershootingspace

Shooting in multiplayer?

Hello so far i have got multiplayer working correctly, now in my player script i created a shooting function, it works with one problem, if there is more than 1 person playing when one person shoots it shoots on both players screens. Even though i check it was the correct players network (checkifmine)

Here is my current script. (Sorry if its messy or just really bad, i'm still learning)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour
 {
     
     public float playerHEALTH = 1000;
     public float playerMAXHEALTH = 1000;
     
     private float lastSynchronizationTime = 0f;
     private float syncDelay = 0f;
     private float syncTime = 0f;
     private Vector3 syncStartPosition = Vector3.zero;
     private Vector3 syncEndPosition = Vector3.zero;
     
     public bool PlayerPaused = false;
     public bool GUIEnabled = false;
     public bool SettingsEnabled = false;
     
     public Camera MyPlayerCam;
     public float speed = 60f;
     public float turnSpeed = 0.15f;
     
     public float fovSliderValue = 0.0F;
     public float RealFOV = 0F;
     
     public GameObject PlayerBullet1;
     public Transform PlayerBulletSpawn;
     public Transform PlayerBulletSpawn2;
  
     void Update()
     {
         if (networkView.isMine)
         {
             MyPlayerCam.enabled = true;
             RealFOV = fovSliderValue + 60;
             MyPlayerCam.fieldOfView = RealFOV;
             InputMovement();
             InputColorChange();
         }
         else
         {
             MyPlayerCam.enabled = false;
             SyncedMovement();
         }
         
         /*float forwardSpeed = Vector3.Dot(rigidbody.velocity, Vector3.forward);
         float rightSpeed = Vector3.Dot (rigidbody.velocity, Vector3.right);
         float upSpeed = Vector3.Dot (rigidbody.velocity, Vector3.up);
         //FOWARDBACK
         if (forwardSpeed > 0) 
         {
             rigidbody.AddRelativeForce (Vector3.back * 26);
         }
         else if (forwardSpeed < 0)
         {
             rigidbody.AddRelativeForce (Vector3.forward * 26);
         }
         //SIDE
         if (rightSpeed > 0)
         {
             rigidbody.AddRelativeForce (Vector3.left * 26);
         }
         else if (rightSpeed <0)
         {
             rigidbody.AddRelativeForce (Vector3.right * 26);
         }
         //UPDOWN
         if (upSpeed > 0)
         {
             rigidbody.AddRelativeForce (Vector3.down * 26);
         }
         else if (upSpeed < 0)
         {
             rigidbody.AddRelativeForce (Vector3.up * 26);
         }*/
     }
 
     void Start()
     {    
         Screen.lockCursor = true;
     }
  
     private void SyncedMovement()
     {
         syncTime += Time.deltaTime;
         rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
     }
     
     private void InputColorChange()
     {
         if (Input.GetKeyDown(KeyCode.R))
             ChangeColorTo(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)));
     }
      
     [RPC] void ChangeColorTo(Vector3 color)
     {
         renderer.material.color = new Color(color.x, color.y, color.z, 1f);
      
         if (networkView.isMine)
             networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
     }
  
     void InputMovement()
     {
         if (Input.GetKey(KeyCode.W))
             rigidbody.AddRelativeForce (Vector3.forward * speed);
  
         if (Input.GetKey(KeyCode.S))
              rigidbody.AddRelativeForce (Vector3.back * speed);
  
         if (Input.GetKey(KeyCode.D))
             rigidbody.AddRelativeForce (Vector3.right * speed);
         
         if (Input.GetKey(KeyCode.A))
             rigidbody.AddRelativeForce (Vector3.left * speed);
         
         if (Input.GetKey(KeyCode.Space))
             rigidbody.AddRelativeForce (Vector3.up * speed);
         
         if (Input.GetKey(KeyCode.LeftControl))
             rigidbody.AddRelativeForce (Vector3.down * speed);
  
         rigidbody.AddRelativeTorque ((Input.GetAxis("Vertical"))*turnSpeed,0,0);
         rigidbody.AddRelativeTorque (0,(Input.GetAxis("Horizontal"))*turnSpeed,0);
         
         if (Input.GetKey (KeyCode.Escape))
             PlayerPaused = !PlayerPaused;
         
         if (Input.GetKey (KeyCode.Mouse1))
         {
            Shot();
         }
         
     }
 
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         Vector3 syncPosition = Vector3.zero;
         Vector3 syncVelocity = Vector3.zero;
         if (stream.isWriting)
         {
             syncPosition = rigidbody.position;
             stream.Serialize(ref syncPosition);
      
             syncVelocity = rigidbody.velocity;
             stream.Serialize(ref syncVelocity);
         }
         else
         {
             stream.Serialize(ref syncPosition);
             stream.Serialize(ref syncVelocity);
      
             syncTime = 0f;
             syncDelay = Time.time - lastSynchronizationTime;
             lastSynchronizationTime = Time.time;
      
             syncEndPosition = syncPosition + syncVelocity * syncDelay;
             syncStartPosition = rigidbody.position;
         }
     }
 
     void OnGUI () {
         if(PlayerPaused)
         {    
             GUI.Box(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 225,250,350), "Player Menu");
             if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 180, 250, 20), "Settings")) {
                     SettingsEnabled = !SettingsEnabled;
             }
             if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 205, 250, 20), "Disconnect")) {
                 PlayerPaused = false;
                 Network.Destroy(gameObject);
                 Network.Disconnect();
                 MasterServer.UnregisterHost();
             }
         }
         
         if(SettingsEnabled)
         {
             GUI.Box(new Rect(Screen.width / 2 + 160, Screen.height / 2 - 225,250,350), "Settings Menu");    
             GUI.Label(new Rect(Screen.width / 2 + 170, Screen.height / 2 - 200, 100, 20), "FOV: " + RealFOV);
             fovSliderValue = GUI.HorizontalSlider(new Rect(Screen.width / 2 + 270, Screen.height / 2 - 195, 100, 30), fovSliderValue, 0.0F, 40.0F);
         }
     }
     
     void Shot()
     {
         GameObject pel = (GameObject)Instantiate(PlayerBullet1, PlayerBulletSpawn.position, PlayerBulletSpawn.rotation);
         pel.rigidbody.AddForce(transform.forward * 9000);
         GameObject pel2 = (GameObject)Instantiate(PlayerBullet1, PlayerBulletSpawn2.position, PlayerBulletSpawn2.rotation);
         pel2.rigidbody.AddForce(transform.forward * 9000);
         
     }
 }
 
Comment
Add comment · Show 1
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
avatar image nastasache · Jan 09, 2014 at 11:24 AM 0
Share

I see no problem with your code; are you sure the owner of network view is the local player? Do you used Instantiate() with RPC or Network.Instantiate() for the player? That may make the owner difference.

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

19 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

Related Questions

Adding knockback to 2D multiplayer game 1 Answer

Make that muzzle flash? How? 1 Answer

Multiplayer FPS Bullets: should they be rigid bodies or raycast? 1 Answer

i get an error when the host shots the client in Unet 0 Answers

Floating Origin and Multiplayer 2 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