Hi, I’m having issues right now with my current project causing huge CPU usage only when someone in my scene is moving. if everyone stands still then the CPU usage will go down to a more desirable amount. Below I have a picture of the game in play mode and my entire movement script as well. This problem does also occur when just another player is moving around in the scene and not the main player. I’ve tried to look up some potential fixes but nothing really useful popped up. from what I understand; the way I’ve got my movement script shouldn’t affect performance.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movement : MonoBehaviour
{
#region Variables
public Main main;
private static GameObject player;
private Rigidbody rbody;
private float moveSpeed;
private int jumps;
private int maxJumpCombo;
private bool canWallJump = true;
private Collision col;
private GameObject jumpCloud;
private static GameObject aJumpCloud;
private static int nClouds;
private Animator anim;
private int jumpStateHash;
private int runStateHash;
private AnimatorStateInfo stateInfo;
private Keybinds Keybinds;
Vector3 movement;
bool isPaused;
#endregion
#region Initiation
void Start()
{
#region Main
main = GameObject.Find("Main").GetComponent<Main>();
Keybinds = main.GetComponent<Keybinds>();
#endregion
#region Player stuff
player = main.Player;
rbody = player.GetComponent<Rigidbody>();
rbody.freezeRotation = true;
movement = new Vector3(0, 0, 0);
#endregion
#region Local variables
moveSpeed = main.Speed;
maxJumpCombo = main.Jumps;
#endregion
#region Animation
anim = GetComponent<Animator>();
jumpStateHash = Animator.StringToHash("Base Layer.Jump");
runStateHash = Animator.StringToHash("Base Layer.Running");
player.GetComponent<Animator>().SetBool("Grounded", true);
#endregion
}
#endregion
#region Movement
void Update()
{
isPaused = main.isPaused;
if (!isPaused)
{
float xAxis = 0;
if (Input.GetKey(main.keybinds["Left"]))
{
if (xAxis != -1)
xAxis = -1;
}
else if (Input.GetKey(main.keybinds["Right"]))
{
if (xAxis != 1)
xAxis = 1;
}
else
{
if (xAxis != 0)
xAxis = 0;
}
#region xAxis always positive
if (xAxis < 0)
{
anim.SetFloat("xAxis", xAxis * -1);
}
else if (xAxis > 0)
{
anim.SetFloat("xAxis", xAxis);
}
else
{
anim.SetFloat("xAxis", 0);
}
#endregion
#region Turning character
if (Input.mousePosition.x > (Screen.width / 2))
{
player.transform.rotation = Quaternion.Euler(0, 90, 0);
main.client.gameObject.GetComponent<ClientMovement>().rotation = 90;
}
else if (Input.mousePosition.x < (Screen.width / 2))
{
player.transform.rotation = Quaternion.Euler(0, 270, 0);
main.client.gameObject.GetComponent<ClientMovement>().rotation = 270;
}
#endregion
#region Movement
movement.x = xAxis;
movement = movement * moveSpeed * Time.deltaTime;
rbody.MovePosition(transform.position + transform.forward * Time.deltaTime);
#endregion
#region Jumping and wall-jumping
if (Input.GetKeyDown(main.keybinds["Jump"]))
{
if (jumps < maxJumpCombo)
{
rbody.velocity = new Vector3(rbody.velocity.x, main.JumpHeight, 0f);
anim.Play(jumpStateHash);
createJumpCloud();
jumps++;
}
else if (canWallJump)
{
if (col.transform.position.x < player.transform.position.x)
{
rbody.velocity = new Vector3(3, main.JumpHeight, 0f);
anim.Play(jumpStateHash);
createJumpCloud();
}
else if (col.transform.position.x > player.transform.position.x)
{
rbody.velocity = new Vector3(-3, main.JumpHeight, 0f);
anim.Play(jumpStateHash);
createJumpCloud();
}
jumps--;
canWallJump = false;
}
}
#endregion
#region Clouds
if (Input.GetKeyDown(main.keybinds["Jump"]))
{
nClouds = 0;
}
#endregion
}
}
#endregion
#region Cloud Creation
private static void createJumpCloud()
{
if (nClouds == 0)
{
Cloud c = new Cloud(player);
}
nClouds = 1;
}
#endregion
#region Collision Enter/Exit
void OnCollisionEnter(Collision c)
{
col = c;
if (c != null)
{
if (col.gameObject.tag == "Floor")
{
player.GetComponent<Animator>().SetBool("Grounded", true);
jumps = 0;
}
if (c.gameObject.tag == "Wall" && c.gameObject != null)
{
canWallJump = true;
}
}
}
void OnCollisionExit()
{
player.GetComponent<Animator>().SetBool("Grounded", false);
canWallJump = false;
}
#endregion
}
The way I have the networked players setup is much simpler than my movement script. The way I have my networked players is like this (Note I have made a custom server to handle this kind of behavior but I don’t think its my server because this fires constantly even if everyone is standing still): client sends name and position to server > server stores this information > server collects and sends other player’s ‘saved information about position’ back to the client > client applies new position to each player’s respective object.