Need help with a (probably) simple rotation issue

In my 2D game that has a view from the side like mario brothers, I’m attempting to make it so that the the character’s gun rotates to look at the mouse, but I’m also trying to limit the rotation to a single axis (x) in order to save on bandwidth because the game will eventually be online.

I kind of have the script working, but when the mouse is on the left side of the character, the gun freaks out and spastically moves between both sides of the character.

I have the script attached to a single gameobject attached to the character that alters it’s rotation.x to look towards the mouse. (The gun is attached to this point)

function Update () 
{
	var target = Vector3(camera.main.ScreenToWorldPoint(Input.mousePosition).x, camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0);
	   
	if (target.x >= gameObject.transform.position.x) transform.rotation.eulerAngles.x = Quaternion.LookRotation(transform.position - target).eulerAngles.x;
	
	else  transform.rotation.eulerAngles.x = -(Quaternion.LookRotation(transform.position - target).eulerAngles.x +180);
}

As you can see, when the mouse is on the left side of the character I’m attempting to just add 180 degrees to the rotation point’s rotation, but the game doesn’t like this for some reason and constantly tries to reverse the rotation. Any ideas on this?

Don’t worry about limiting the rotation due to bandwidth. It doesn’t take any bandwidth (it’s all logical processing) and it’s already ridiculously fast.

But when online the server/other players need to know what direction your gun is facing, and they can’t know that unless they know its rotation?

I don’t understand how the server can ‘just know’ where your gun is pointing without receiving that data from the client.

Or is the data compressed somehow so that the data size wouldn’t change if I just sent the whole transform.rotation data rather than just sending one axis?

Sorry, I didn’t realize you were building a multiplayer game. Again though, I would not be too concerned with bandwidth when it comes to passing one or two extra floating point numbers. Focus first on building simple code that is easy to work with and maintain. Once your game is working, then at that point start benchmarking, and profiling to find your bottlenecks or issues to optimize because you’ll know what exactly you need to fix and you’ll have a working game baseline to start from.

My game is pretty well working right now, I’m just attempting to optimize it as much as possible from the ground up so I don’t have to worry about having to make big changes later.

For what it’s worth, that’s not necessarily the best way to go about it. If you try to ‘optimize everything from the ground up’, you’re likely to invest a significant amount of effort optimizing things that end up not mattering much performance-wise. That’s why it’s usually recommended to make it work first, and then, if performance problems arise, use profiling and other performance metrics to determine where changes need to be made. (That’s not to say you shouldn’t keep performance in mind from the get-go, but ‘optimizing as much as possible from the ground up’ sounds a bit overboard.)

I don’t know if you’ve fixed your rotation problem, but I can see a few potential problems in the code you posted.

First, it should probably be ‘target - transform.position’, not ‘transform.position - target’. Also, if the method used to generate the rotation is sound, you shouldn’t have to have any conditionals of the kind shown in your code (‘target.x >= gameObject.transform.position.x’ in this case).

Finally, if the rotation is locked to one of the cardinal planes (e.g. xy, yz, etc.), you could just use Atan2() to compute the rotation angle directly.

In any case, I’d recommend implementing this in the most straightforward way possible and getting it working first before worrying about any possible optimizations.