- Home /
Authoritative Rotation
Hi Guys,
The question I have about this is more geared toward the math... as I have not been able to nail down the correct formula for rotating the turret of my tank.
First, I DO have rotation working across an authoritative server/client configuration so that is not the problem I am having. The problem I am having, is the point at which the calculates the rotation from.
The player can move using the standard WASD and it is top down. The player uses the mouse to "Aim" the turret of the tank(no rotation speed, it simply snaps to place) and it should point toward the mouse location based on the mouse position on the screen, and I think that is where I am running in to problems.
I guess lets just post the code instead of me rambling.
private void RotateTurret() { if (owner != null && Network.player == owner) { Vector3 mousePosition = Input.mousePosition;
if (clientLastMousePosition != mousePosition)
{
clientLastMousePosition = mousePosition;
if (Network.isServer)
{
UpdateMousePosition(mousePosition);
}
else if (Network.isClient)
{
networkView.RPC("UpdateMousePosition", RPCMode.Server, mousePosition);
}
}
}
if (Network.isServer)
{
// find vector to the mouse
Vector3 tempVector2 = new Vector3(myTransform.position.x, 0, myTransform.position.z);
//Vector3 tempVector2 = new Vector3(Screen.width * 0.5f, 0, Screen.height * 0.5f); // the position of the middle of the screen
//Vector3 tempVector2 = myTransform.position;
Vector3 tempVector = serverCurrentMousePosition; // find the position of the mouse on screen
//// input mouse position gives us 2D coordinates, I am moving the Y coordinate to the Z coorindate in temp Vector and setting the Y
//coordinate to 0, so that the Vector will read the input along the X (left and right of screen) and Z
//(up and down screen) axis, and not the X and Y (in and out of screen) axis
tempVector.z = tempVector.y;
tempVector.y = 0;
//Debug.Log(tempVector);
// the direction we want face/aim/shoot is from the middle of the screen to where the mouse is pointing
Vector3 inputRotation = tempVector - tempVector2;
turret.rotation = Quaternion.LookRotation(inputRotation);
Debug.DrawRay(target.position, turret.forward);
}
}
[RPC] private void UpdateMousePosition(Vector3 mousePosition) { serverCurrentMousePosition = mousePosition; }
As you can see there are a lot of commented out lines, as I have been trying everything. And what is happening is the "point" at which it calculates the rotation is down and to the left(screen wise) from the center(where the player is).
I assume it is because the server "screen" sees something different. Any help on this is appreciated.
Thank you
EDIT: I don't know that it makes a difference, but the server is running in Batchmode. It might because as i recall before we started running it like that, it worked ok.
Answer by uhahaha · Dec 29, 2010 at 03:52 AM
If your problem concerns only with
"The player uses the mouse to "Aim" the turret of the tank(no rotation speed, it simply snaps to place) and it should point toward the mouse location based on the mouse position on the screen, and I think that is where I am running in to problems."
then you can apply the following Wiki script to your turret:
http://www.unifycommunity.com/wiki/index.php?title=LookAtMouse
Answer by Azound · Feb 22, 2011 at 07:28 AM
It looks like the problem is that the mouse position is in screen coordinates, while you game is running in "world" coordinates. You have to convert the mouse position into world coordinates before you do anything with them.
i.e.
Vector3 mousePosition = Camera.current.ScreenToWorldPoint(Input.mousePosition);