Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Echofiend · Dec 28, 2010 at 10:07 PM · rotationmousemathauthoritative

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

[JS]Rotate object y axis to mouse position 1 Answer

Mouse.position from center of player 1 Answer

Changing the forward rotation for LookAt 2 Answers

Rotation problem: Check is failing/ endlessly spins 1 Answer

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges