How to make the stock first person controller able to be used while networking?
Hey everyone, I've been working for over 3 weeks trying to set the stock first person script to be used in my multiplayer game for school. I have it so the two players can see the other spawn in, and they move independently, but they can't see each other move, is there something i'm doing wrong of something i'm not doing? I've included all the stuff i put into the script.
Thank you any help would be amazing!
using System; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; using UnityStandardAssets.Utility; using Random = UnityEngine.Random; using UnityEngine.Networking;
namespace UnityStandardAssets.Characters.FirstPerson { [NetworkSettings(channel = 0)] [RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(AudioSource))]
public class FirstPersonNetworkMovement : NetworkBehaviour
{
[SerializeField]
private bool m_IsWalking;
[SerializeField]
private float m_WalkSpeed;
[SerializeField]
private float m_RunSpeed;
[SerializeField]
[Range(0f, 1f)]
private float m_RunstepLenghten;
[SerializeField]
private float m_JumpSpeed;
[SerializeField]
private float m_StickToGroundForce;
[SerializeField]
private float m_GravityMultiplier;
[SerializeField]
private MouseLook m_MouseLook;
[SerializeField]
private bool m_UseFovKick;
[SerializeField]
private FOVKick m_FovKick = new FOVKick();
[SerializeField]
private bool m_UseHeadBob;
[SerializeField]
private CurveControlledBob m_HeadBob = new CurveControlledBob();
[SerializeField]
private LerpControlledBob m_JumpBob = new LerpControlledBob();
[SerializeField]
private float m_StepInterval;
[SerializeField]
private AudioClip[] m_FootstepSounds; // an array of footstep sounds that will be randomly selected from.
[SerializeField]
private AudioClip m_JumpSound; // the sound played when character leaves the ground.
[SerializeField]
private AudioClip m_LandSound; // the sound played when character touches back on ground.
private Camera m_Camera;
private bool m_Jump;
[SyncVar] private float m_YRotation;
private Vector2 m_Input;
private Vector3 m_MoveDir = Vector3.zero;
private CharacterController m_CharacterController;
private CollisionFlags m_CollisionFlags;
private bool m_PreviouslyGrounded;
private Vector3 m_OriginalCameraPosition;
private float m_StepCycle;
private float m_NextStep;
private bool m_Jumping;
private AudioSource m_AudioSource;
public float[] m_MovementArray = new float[5];
const int X = 0, Y = 1, Z = 2, YAW = 3, PITCH = 4;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
m_Camera = Camera.main;
m_OriginalCameraPosition = m_Camera.transform.localPosition;
m_FovKick.Setup(m_Camera);
m_HeadBob.Setup(m_Camera, m_StepInterval);
m_StepCycle = 0f;
m_NextStep = m_StepCycle / 2f;
m_Jumping = false;
m_AudioSource = GetComponent<AudioSource>();
m_MouseLook.Init(transform, m_Camera.transform);
if (!isLocalPlayer)
{
Debug.Log("This isn't the local player!");
return;
}
else
{
Debug.Log("You are who you say you are!");
}
}
void UpdateArray()
{
if (isLocalPlayer)
{
m_MovementArray[X] = this.gameObject.transform.position.x;
m_MovementArray[Y] = this.gameObject.transform.position.y;
m_MovementArray[Z] = this.gameObject.transform.position.z;
m_MovementArray[YAW] = this.gameObject.transform.rotation.x;
m_MovementArray[PITCH] = this.gameObject.transform.rotation.y;
TakefromArray();
}
}
void SyncServer()
{
Debug.Log("Server Syncing");
}
void TakefromArray()
{
this.gameObject.transform.position = new Vector3(m_MovementArray[X], m_MovementArray[Y], m_MovementArray[Z]);
this.gameObject.transform.rotation = new Quaternion(m_MovementArray[YAW], m_MovementArray[PITCH], 0, 0);
SyncServer();
}
// Update is called once per frame
private void Update()
{
if (isLocalPlayer)
{
UpdateArray();
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;
}
else
{
Debug.Log("You have no power Here");
return;
}
}
private void PlayLandingSound()
{
m_AudioSource.clip = m_LandSound;
m_AudioSource.Play();
m_NextStep = m_StepCycle + .5f;
}
private void FixedUpdate()
{
if (isLocalPlayer)
{
float speed;
GetInput(out speed);
// always move along the camera forward as it is the direction that it being aimed at
Vector3 desiredMove = transform.forward * m_Input.y + transform.right * m_Input.x;
// get a normal for the surface that is being touched to move along it
RaycastHit hitInfo;
Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
m_CharacterController.height / 2f, ~0, QueryTriggerInteraction.Ignore);
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
m_MoveDir.x = desiredMove.x * speed;
m_MoveDir.z = desiredMove.z * speed;
if (m_CharacterController.isGrounded)
{
m_MoveDir.y = -m_StickToGroundForce;
if (m_Jump)
{
m_MoveDir.y = m_JumpSpeed;
PlayJumpSound();
m_Jump = false;
m_Jumping = true;
}
}
else
{
m_MoveDir += Physics.gravity * m_GravityMultiplier * Time.fixedDeltaTime;
}
m_CollisionFlags = m_CharacterController.Move(m_MoveDir * Time.fixedDeltaTime);
ProgressStepCycle(speed);
UpdateCameraPosition(speed);
m_MouseLook.UpdateCursorLock();
}
Debug.Log("This is not what we want");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612070157im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
MultipartFormDataSection not working - Error: 403 0 Answers
ClientRpc not functioning 0 Answers
Unity Networking 0 Answers
Check what group id you have after being instantiated. (PUN) 1 Answer