- Home /
Mouselook problem multiplayer. Synchronized in 2 computers.
First, sorry for my english. Hello, I have a problem with mouselook in multiplayer game. Both players can move others first person cameras. I fixed movement by using other script which recognize local player and turns on movement script for him, but i cant make the same with camera player. My code for movement looks like this (dont look at comments. Im from poland and its helps my understanding code a little bit better!)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementAwatara : MonoBehaviour {
public float speed = 10.0F; //multiplayer speeda
static Animator anim;
void Start () {
Cursor.lockState = CursorLockMode.Locked; //zablokowanie kursora
anim = GetComponent<Animator>();
}
void Update () {
float horizontalValue = Input.GetAxis ("Horizontal"); //float znajdujący przycisk w prawo i w lewo
float verticalValue = Input.GetAxis ("Vertical"); //float znajdujący przycisk w góre i w dół
float translation = Input.GetAxis ("Horizontal") * speed; //float znajdujący przycisk w prawo i w lewo + mnożenie przez multiplayer speeda
float straffe = Input.GetAxis ("Vertical") * speed; //float znajdujący przycisk w góre i w dół + mnożenie przez multiplayer speeda
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(translation, 0, straffe);
//Chodzenie (Komunikaty i opcje)
if(horizontalValue < 0){ //przycisk w lewo (ładujący się)
print ("lewo"); //napisz w konsoli unity "lewo"
}
if(horizontalValue > 0){ //przycisk w prawo (ładujący się)
print ("prawo");
}
if(verticalValue < 0){ //przycisk w dół (ładujący się)
print ("dół");
}
if(verticalValue > 0){ //przycisk w góre (ładujący się)
print ("góra");
}
if(Input.GetKeyDown("escape")){ //jeżeli kliknięty jest klawisz "escape"
Cursor.lockState = CursorLockMode.None; //odblokuj kursor
}
//Chodzenie
if(straffe > 0){
anim.SetBool("Walk", true);
}
else
{
anim.SetBool("Walk", false);
}
if(straffe < 0){
anim.SetBool("BackWalk", true);
}
else
{
anim.SetBool("BackWalk", false);
}
if(translation < 0){
anim.SetBool("LeftWalk", true);
}
else
{
anim.SetBool("LeftWalk", false);
}
if(translation > 0){
anim.SetBool("RightWalk", true);
}
else
{
anim.SetBool("RightWalk", false);
}
//Dodatkowe
if(Input.GetKeyDown("space")){
anim.SetTrigger("Jump");
}
if(Input.GetKeyDown("j")){
anim.SetTrigger("HouseDance");
}
}
}
Thats code for my mouse looking script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementMouse : MonoBehaviour {
Vector2 mouselook;
Vector2 smoothV;
public float sensitivity = 3.0F;
public float smoothing = 2.0F;
GameObject character;
void Start () {
character = this.transform.parent.gameObject;
}
void Update () {
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
mouselook += smoothV;
transform.localRotation = Quaternion.AngleAxis(-mouselook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis(mouselook.x, character.transform.up);
}
}
And this is my script for multiplayer per player movement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SterowanieMultiplayerowe : NetworkBehaviour {
void Start () {
if(isLocalPlayer){
GetComponent<MovementAwatara>().enabled = true;
}
}
}
Can somemody help my with this? I will be very happy if someone can! I can explain better and deliver more informations/pictures/code if someone want to help me! Thanks!
Answer by zigol01 · Aug 11, 2017 at 01:28 PM
The problem is i cant have 2 NetworkIdentity for camera and my avatar. It says its not supported. I dont know how to solve this.
Answer by Phoenix3666 · Jul 17, 2018 at 05:35 AM
I'm trying to figure out how to get around this too, if you find out how, lmk