EDDIT: fixed it now, check reply
Hello all
Im trying to create a multiplayer game and im having a few problems with the networking side. i have use the unity multiplayer tools like the network manager for example. When creating the server the person view looks fine however when a second person joins the player is constantly moving around and “glitching around the place” even tho there not moving. i followed the original tutorial to creating a multiplayer game but i wanted to change the movement as it was crap in the tutorial so i followed a tutorial to add mouse movement for look and wasd to move, sprinting and jumping. Im not to sure what is causing the problem but i guess its something to do with the way the script it. ill add the sript and a gyazo giff of what it looks like ingame
using UnityEngine;
using UnityEngine.Networking;
//remnove 1
public class PlayerController : NetworkBehaviour
{
//shooting
public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public Camera fpscam;
private float nextTimeToFire = 0f;
//sound
public AudioClip FireFX;
public AudioSource Fxsource;
public GameObject bulletPrefab;
public Transform bulletSpawn;
//movement
public float speed = 5f;
private bool sprinting = false, hasJumped, isCrouched;
public float sensitivity = 2f;
float moveFB, moveLR, rotX, rotY,vertVelocity;
public float jumpForce = 4;
CharacterController player;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
player = GetComponent<CharacterController>();
}
void Update()
{
if (!isLocalPlayer)
{
return;
}
//moving
//var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
//var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
//transform.Rotate(0, x, 0);
//transform.Translate(0, 0, z);
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 2f / fireRate;
CmdFire();
Fxsource.clip = FireFX;
Fxsource.Play();
}
moveFB = Input.GetAxis("Vertical") * speed;
moveLR = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY = Input.GetAxis("Mouse Y") * sensitivity;
//0 is jump
Vector3 movement = new Vector3(moveLR, vertVelocity, moveFB);
transform.Rotate(0, rotX, 0);
fpscam.transform.Rotate(-rotY, 0, 0);
movement = transform.rotation * movement;
player.Move(movement * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = speed *2;
sprinting = true;
}
if (sprinting == true && Input.GetKeyUp(KeyCode.LeftShift))
{
speed = speed / 2;
sprinting = false;
}
if (Input.GetKey(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
if (Input.GetButtonDown("Jump"))
{
hasJumped = true;
}
if (Input.GetButtonDown("Crouch"))
{
if (isCrouched == false)
{
player.height = player.height / 2;
isCrouched = true;
}
else
{
player.height = player.height * 2;
isCrouched = false;
}
}
ApplyGravity();
}
private void ApplyGravity()
{
if (player.isGrounded == true)
{
if (hasJumped == false)
{
vertVelocity = Physics.gravity.y;
}
else
{
vertVelocity = jumpForce;
}
}
else
{
vertVelocity += Physics.gravity.y * Time.deltaTime;
vertVelocity = Mathf.Clamp(vertVelocity, -50f, jumpForce);
hasJumped = false;
}
}
// This [Command] code is called on the Client …
// … but it is run on the Server!
[Command]
void CmdFire()
{
RaycastHit hitray;
if (Physics.Raycast(fpscam.transform.position, fpscam.transform.forward, out hitray, range))
{
var hit = hitray.transform.gameObject;
var health = hit.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(20);
}
}
// 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 * 6;
// 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.blue;
}
}