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 AaronWFinn · Jun 12, 2015 at 06:49 PM · c#movementmouse

How to make the turret of a tank in a 3rd person game look at the mouse cursor?

I have a tank model in Unity, it has a rig and animations that move the tread and opens and closes a door. I a have a simple move script on the tank and a follow script on the camera that follows the tank. What I want to do is have it to where the turret will aim at wherever the cursor looks at. I tried to put a capsule collider on the turret and then add a mouse look at script to it but it's not working. What could I do to achieve this?

Comment
Add comment · Show 1
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 savlon · Jun 12, 2015 at 06:57 PM 0
Share

Go to http://www.savalishunitytutorials.com and look at the rotate to mouse script. It's in 2D but substitute the y axis for the z axis and you should be right

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jun 12, 2015 at 08:08 PM

I find the best way to do things like this is to think about it realistically, and work from there.

With an actual tank turret, they generally have two separate pivot points. The first moves the whole 'head' around the Y-axis, allowing the turret to turn 360 degrees around the tank. The second pivot is usually near where the barrel starts, and allows the barrel to be pitched up or down a limited amount.

So, you want to set up your scene as such. Include an empty GameObject in the hierarchy for each pivot and store a reference to each.

Then you want to take the Transform.forward vector from the camera and get the barrel to match that as close as you can using the provided pivots and constraints. You're gonna need a little trig here:

First, you want to find the yaw for the first pivot. I suggest feeding in the transform.forward's x and z components to Mathf.Atan2 to get an angle in radians. Convert it to degrees and apply it as a y-axis euler rotation to the first pivot.

Next, you want to find the pitch. First you'll need the combined horizontals for the vector, using the Pythagorean equation with the x and z components. Then you can just use Atan2 again with the y component and the x/z combined horizontal component to get the pitch in radians. Depending on how your second pivot is oriented, you can apply that with another simple local euler rotation on the second pivot. Since the second pivot is a child of the first pivot, it will inherit it's transform and combined, your turret will be pointing the correct direction.

Comment
Add comment · Show 1 · 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 AaronWFinn · Jun 15, 2015 at 02:30 AM 0
Share

oh okay that makes sense, I had it rigged that way but I'll have to try what you suggested. Thanks for the advice.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How do I make my character lean to the sides? 0 Answers

WSAD controls work fine for W and S, but randomly get reversed for A and D 0 Answers

How to properly set the position clicked by the mouse? 0 Answers

Unity's mouse look causing movement issues 1 Answer


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