- Home /
Question by
iGetty1 · Apr 11, 2017 at 05:42 PM ·
multiplayer-networkingmouselook
Multiplayer MouseLook controlling other cameras.
As you can see on the video, there are strange things happening.
For some reason, I am controlling the wrong character via multiplayer mode..
Video: https://www.youtube.com/watch?v=8Vozt8BPB3U&feature=youtu.be
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class CharControl : NetworkBehaviour {
public float moveSpeed = 4.0f;
public bool isSprinting = false;
Vector2 _mouseAbsolute;
Vector2 _smoothMouse;
public Vector2 clampInDegrees = new Vector2(360, 180);
public bool lockCursor;
public Vector2 sensitivity = new Vector2 (2, 2);
public Vector2 smoothing = new Vector2(3, 3);
public Vector2 targetDirection;
void Start () {
if (!isLocalPlayer)
return;
targetDirection = transform.localRotation.eulerAngles;
}
void Update () {
if (!isLocalPlayer)
{
return;
}
float moveForwBack = Input.GetAxis ("Vertical") * moveSpeed;
float moveSideSide = Input.GetAxis ("Horizontal") * moveSpeed;
moveForwBack *= Time.deltaTime;
moveSideSide *= Time.deltaTime;
transform.Translate (moveSideSide, 0, moveForwBack);
CmdSprinting ();
var targetOrientation = Quaternion.Euler (targetDirection);
var mouseDelta = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
mouseDelta = Vector2.Scale (mouseDelta, new Vector2 (sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
_smoothMouse.x = Mathf.Lerp (_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
_smoothMouse.y = Mathf.Lerp (_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
_mouseAbsolute += _smoothMouse;
if (clampInDegrees.x < 360)
_mouseAbsolute.x = Mathf.Clamp (_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
var xRotation = Quaternion.AngleAxis (-_mouseAbsolute.y, targetOrientation * Vector3.right);
transform.localRotation = xRotation;
if (clampInDegrees.y < 360)
_mouseAbsolute.y = Mathf.Clamp (_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
transform.localRotation *= targetOrientation;
var yRotation = Quaternion.AngleAxis (_mouseAbsolute.x, transform.InverseTransformDirection (Vector3.up));
transform.localRotation *= yRotation;
}
[Command]
void CmdSprinting()
{
if (!isLocalPlayer)
return;
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.LeftShift))
{
isSprinting = !isSprinting;
if (isSprinting)
{
moveSpeed = 8.0f;
}
else
{
moveSpeed = 4.0f;
}
}
}
}
Comment
Would Ricewind1's answer to this question help you in any way?
Also, reading about the NetworkView might help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612115600im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiplayer gameboard: Each player can see the mouse pointer moving over the network 0 Answers
Is there a complete tutorial for Nodejs with unity using Socket.io? 2 Answers
How to sync transform of character's bone(spine)? 0 Answers
How to make networked interactable objects for vive vr? 0 Answers
PhotonNetwork.Instantiate ? 0 Answers