Hello, I’m making a 2D co-op game, with puzzles that involve moving physics objects.
One of the weapons the player has is a weapon that imitates the behaviour of the Half-Life 2 Gravity Gun;
- You right click in a certain direction
- It makes a raycast in that direction, with a limit distance
- If it found a gameobject with a collider and a certain tag, it stores it as “grabbedObject”
- It sets the grabbedObject position making it float in front of the player
- You right click again and you drop it.
That works fine on the Host, and as client you can see the box move.
As a client, when you pick up a box, it sends the info to the host and this one gets a FPS drop (from 90 to 60 ~)
Script 1 - In the player (SyncGrabbedObject.cs)
void FixedUpdate () {
if(grabbedObject != null){
if(isLocalPlayer && !isServer){
timer++;
if (timer>= 4) {
Debug.Log("Hey, we're grabbing an object as a LocalPlayer");
clientPos = grabbedObject.position; // Vector2 used for storing the data to send
CmdUpdPos(grabbedObject.gameObject, clientPos, grabbedObject.GetComponent<GrabbedObject>().isBeingDragged);
timer= 0;
}
}
}
}
[Command] public void CmdUpdPos(GameObject grabbedObjectR, Vector2 clientPosR, bool isBDragged){
Debug.Log("Hey, we're being told that we should UpdPos");
grabbedObjectR.GetComponent<GrabbedObject>().isBeingDragged = isBDragged;
grabbedObjectR.GetComponent<GrabbedObject>().LerpPosition(clientPosR);
}
Script 2 - in the Box (GrabbedObject.cs)
public void LerpPosition(Vector2 clientPosR){
gameObject.GetComponent<Transform>().position = Vector2.Lerp(gameObject.GetComponent<Transform>().position, clientPosR, Time.deltaTime * lerpRate);
Debug.Log ("Hey, we're UpdPos!");
}
Is there other better way to update the position?