- Home /
Unity Multiplayer FPS shooting issue.
So im working on an FPS multiplayer project to further build my knowledge on the engine. I ran into a weird issue when spawning two players into the same game. The host of the game operates normally and as it should. It shoots from the correct position which is the end of the barrel of the gun prefab. The client that connects however can shoot. But its not shooting from the correct location. It picks up the X, and Z axis aiming and shooting fine. But when I lift my mouse up on the Y axis, the cursor and crosshair all move. But the shooting of the prefab just goes straight out. I cant shoot in the air. Only straight. Do keep in my mind im using the FPS Controller Unity provides. Also trying to follow the tutorial they have posted for networking. Here is some the code for the shooting.
private void Update()
{
if (!isLocalPlayer)
{
return;
}
RotateView();
// the jump state needs to read here to make sure it is not missed
if (!m_Jump)
{
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
}
if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
{
StartCoroutine(m_JumpBob.DoBobCycle());
PlayLandingSound();
m_MoveDir.y = 0f;
m_Jumping = false;
}
if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
{
m_MoveDir.y = 0f;
}
m_PreviouslyGrounded = m_CharacterController.isGrounded;
if (Input.GetMouseButtonDown(0))
{
CmdFireWeapon();
}
}
[Command]
public void CmdFireWeapon()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
shootPoint.position,
shootPoint.rotation);
// Add velocity to the bullet
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 300;
NetworkServer.Spawn(bullet);
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
Im not sure why the rotation when I shoot isnt updating. I was thinking it might have something to do with the mouselook that comes with the Standard assets.. Anyone have any idea what is happening, and why the host operates fine, but the client that connects to the host is not?
It is a GameObject attached to the weapon itself yes. Located at the end of the barrel and is attached which is confirmed. As I said its just really weird that the host works 100& proper. The client is what is messing up. It has to be a networking problem on my end.
the problem is that the actual shooting is happening on the server. it doesn't know where the client is looking at and just instantiates the bullet with its own local coordinates. if you add the actual rotation of the client to the command, the server can instantiate the bullet with the correct orientation.
Where would it be that I add that rotation update? Ive tried a few different things and all have failed. Im brand new to networking and stuff so this is all new to me lol
add a quaternion parameter to CmdFireWeapon and pass it the necessary rotation. use it IN the command on the server side
Your answer
Follow this Question
Related Questions
Unity 5.2 Unet, extreme Lags after updating from 5.1 1 Answer
Is Unet too heavy for mobile games? 0 Answers
Unity Multiplayer Oyun Yapma 0 Answers
Multiplayer Respawning Problems 0 Answers