- Home /
Question by
wermon · Dec 29, 2017 at 07:21 PM ·
c#networkingmultiplayernetworkcodepage
Multiplayer desynchronize when grabbing an object by code.
I'm having trouble getting the movement of my object synchronized. It is an object which is grasped but, if the host moves it, it looks normal in both the host and the client. But if I move it on the client, the host does not receive the information generated by the client.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Agarrar : NetworkBehaviour {
public Transform center = null;
public float reach = 3f;
public float rate = 0.45f;
public float chargeRate = 2f;
public float maxThrowForce = 5f;
public float grabCooldown = 1f;
public float repositionRate = 50f;
public float rotateRate = 10f;
public float minimumDistance = 1f;
public float maximumDistance = 3f;
public bool canGrab = true;
public bool canThrow = true;
public bool canPush = true;
public bool canReposition = true;
public bool canRotate = true;
public LayerMask layerMask;
public string liftTag = "Liftable";
private float wantedPosition = 3f;
private float grabTimer = 0f;
private float throwForce = 0f;
private bool wantGrab = false;
private bool wantThrow = false;
private bool wantPush = false;
private bool wantReposition = false;
public bool wantRotate = false;
private bool canGrabAgain = true;
private GameObject grabbed = null;
public bool isRotating { get; private set; }
void Update ()
{
Debug.Log("Hols");
CmdUpdateInput();
CmdUpdateLogic();
}
//[Command]
//[ServerCallback]
private void CmdUpdateInput()
{
if (Input.GetMouseButton(1) && canGrab)
{
wantGrab = true;
}
else
{
wantGrab = false;
}
if (Input.GetMouseButton(0))
{
Debug.Log ("hola");
if(throwForce < maxThrowForce)
throwForce += (chargeRate * Time.deltaTime);
if (throwForce > maxThrowForce)
throwForce = maxThrowForce;
}
if (Input.GetMouseButtonUp(0))
{
if (canPush && grabbed == null)
wantPush = true;
if (canThrow && grabbed != null)
wantThrow = true;
}
if (Input.GetAxisRaw("Mouse ScrollWheel") != 0 && canReposition)
{
wantReposition = true;
}
else
{
wantReposition = false;
}
if (Input.GetMouseButtonDown(2) && grabbed != null && canRotate)
{
wantRotate = true;
}
else
{
wantRotate = false;
}
}
//[Command]
private void CmdUpdateLogic()
{
if (wantGrab && canGrabAgain)
{
if (grabbed == null)
{
Ray ray = new Ray(center.position, center.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, reach, layerMask))
{
if (hit.collider.tag == liftTag)
{
grabbed = hit.collider.gameObject;
}
}
}
else
{
if (grabbed.GetComponent<Rigidbody>())
{
Rigidbody rb = grabbed.GetComponent<Rigidbody>();
rb.velocity = ((center.position + (center.forward * wantedPosition)) - grabbed.transform.position) * rate;
}
else
{
grabbed = null;
}
}
}
else
{
if (grabbed != null)
{
grabbed = null;
}
}
if (wantReposition)
{
if (grabbed != null)
{
wantedPosition += (Input.GetAxis("Mouse ScrollWheel") * repositionRate) * Time.deltaTime;
wantedPosition = Mathf.Clamp(wantedPosition, minimumDistance, maximumDistance);
}
else
{
wantReposition = false;
}
}
if (wantRotate)
{
if (grabbed != null)
{
float xa = Input.GetAxis("Mouse X") * 10;
float ya = Input.GetAxis("Mouse Y") * 10;
grabbed.transform.Rotate(new Vector3(ya, -xa, 0), Space.World);
isRotating = true;
}
else
{
isRotating = false;
}
}
else
{
isRotating = false;
}
if (wantThrow)
{
if (grabbed != null)
{
Rigidbody rb = grabbed.GetComponent<Rigidbody>();
rb.velocity += center.rotation * new Vector3(0, 0, throwForce);
throwForce = 0f;
grabbed = null;
canGrabAgain = false;
}
wantThrow = false;
throwForce = 0f;
}
if (wantPush)
{
if (grabbed == null)
{
Ray ray = new Ray(center.position, center.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, reach, layerMask))
{
if (hit.collider.tag == liftTag)
{
Rigidbody rb = hit.collider.transform.GetComponent<Rigidbody>();
rb.velocity = center.rotation * new Vector3(0, 0, throwForce);
throwForce = 0f;
}
}
}
wantPush = false;
throwForce = 0f;
}
if (!canGrabAgain)
{
if (grabTimer < grabCooldown)
{
grabTimer += Time.deltaTime;
}
else
{
canGrabAgain = true;
}
}
else
{
grabTimer = 0f;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Pushed back when applying NetworkTransform 1 Answer
How to call [Command] on Client in UNet 1 Answer
How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers
How to synchronize GameObjects 2 Answers