I wanted to programm a UI-Button which rotates the player by 90 degrees to the left or to the right. The code wouldn’t work unless I made the variables static because the variables would always get rest to the start value.
public Rigidbody rb;
static private Quaternion rotAng;
static private Quaternion targetAng;
static private float rotDir;
private void Start()
{
rb = GetComponent<Rigidbody>();
rotAng = Quaternion.Euler(new Vector3(0, 0, 0));
targetAng = Quaternion.Euler(new Vector3(0, 0, 0));
rotDir = 1;
}
public void FixedUpdate ()
{
if (!isLocalPlayer)
{
return;
}
Debug.Log("rotDir: " + rotDir);
Vector3 target = targetAng.eulerAngles;
Vector3 current = rb.rotation.eulerAngles;
if (Mathf.Abs((target - current).y) > 0.1)
{
Quaternion deltaRotation = Quaternion.Euler(new Vector3(0, rotDir * 90, 0) * Time.deltaTime);
rotAng = rotAng * deltaRotation;
rb.MoveRotation(rotAng);
}
else
{
rb.MoveRotation(targetAng);
}
}
But the game is supposed to be multiplayer. And when I spawn in 2 Players, they both rotate, no matter if it isn’t the local Player, by pressing the button. I assume this is because the variables are static. I can’t find any solution to this problem. So all help is welcome.