x


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.

more ▼

asked Dec 28, 2010 at 10:07 PM

Echofiend gravatar image

Echofiend
18 6 4 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Dec 29, 2010 at 03:52 AM

uhahaha gravatar image

uhahaha
432 7 7 21

(comments are locked)
10|3000 characters needed characters left

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);

more ▼

answered Feb 22, 2011 at 07:28 AM

Azound gravatar image

Azound
713 45 47 67

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3775
x1450
x384
x23

asked: Dec 28, 2010 at 10:07 PM

Seen: 1491 times

Last Updated: Dec 28, 2010 at 10:24 PM