- Home /
Question by
friendlyatheist777 · Dec 06, 2021 at 11:59 AM ·
multiplayeroptimization
Multiplayer problem - any way to optimize script and not copypaste for second player?
target = Camera.main.transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector2(Mouse.current.position.ReadValue().x, Mouse.current.position.ReadValue().y)); // Vector3(Input.GetAxis("AimHorizontal"), Input.GetAxis("AimVertical"), 0.0f);
GameManager.instance.cross.transform.position = new Vector2(target.x, target.y);
Vector3 difference = target - weapon.transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
weapon.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ - 90f);
That's my script for 2D game, that makes crosshair to move with mouse, while making weapon rotates around player. There is also gamepad script which is 3 times bigger. The problem here is when adding another player, i don't know what to do except creating another, absolutely similar method, but change everything to "cross2" and "weapon2", just for player2. Not a biggest problem, but i bet there are better solutions for this. The question is: Is there any more efficient methods to deal with this multiplayer problem? How to optimize my scripts and make it all easier?
Comment