- Home /
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125857im_/https://answers.unity.com/themes/thub/images/avi.jpg)