Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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
1
Question by oktemre · Jul 07, 2015 at 04:48 PM · c#unity 5unity 4.6turrettank

Rotating Tank Turret with correct angle

I have a newbie question

I have a simple tank with a turret on it. I need the turret to point a location pointed by mouse and I've managed to do it with code below:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit = new RaycastHit();
 
 if(Ground.Raycast(ray, out hit, 1000))
 {
     position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
         position.y = 0;
 }
         
 transform.LookAt(position);
 transform.Rotate(Vector3.down, 90f);

The turret is child of the Tank object.

My problem is when the body of the Tank has an angle to the ground. Please see the screenshot

Any advice will be appreciated.

screen-shot-2015-07-07-at-171159.png (83.4 kB)
Comment
Add comment · Show 4
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 Neilos · Jul 07, 2015 at 04:58 PM 0
Share

You don't actually say what the problem is... What do you want to happen?

avatar image oktemre · Jul 07, 2015 at 07:59 PM 0
Share

To be more specific, I need the turret to continue to show the direction of the mouse pointer (on XZ axis) but to stay correctly on the tank's body. Please check the screenshot to see the problem of the turret.

avatar image petey · Jan 06, 2016 at 03:26 AM 0
Share

Hey did you ever figure this out? I'm just trying to find an answer to the same question and it's doing my head in!

avatar image blokkiesotto · Mar 14 at 02:35 PM 0
Share

Can you maybe give me full code and i will try help

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mischa · Jul 07, 2015 at 05:08 PM

Try to set the rotation of your guntower directly. The Quaternion class offers a LookRotation where you can specify an up vector. Something like this should do the trick:

 turretTransform.Rotation = Quaternion.LookRotation(lookPosition - tankPosition, tankUpVector);

Here's the documentation: http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

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 oktemre · Jul 07, 2015 at 08:15 PM 0
Share

Thanks for the feedback. I've tried this and some of LookRotation's variations but it didn't work (or maybe I couldn't manage). The Y value raycasted from the ground is not useful to use a lookat function because body of the tank is not parallel to the ground.

avatar image
0

Answer by Torigas · Jul 08, 2015 at 09:01 AM

I fear you may have to split the rotation in two parts.

First, the turret itself can rotate just around one axis (pretty much transform.up on the turret). Second, the gun can turn around another axis (transform.right).

That means that you have to split the rotation into different components.

You could, for example, use Quaternion.eulerangles. That gives you the pitch, yaw, roll axis.

Using the code from Mischa's answer:

 Transform turret;
 Transform gun;
 
 Vector3 direction = position - transform.position;
 Quaternion rotation = Quaternion.LookRotation(direction);
 Vector3 eulers = rotation.eulerAngles;
 
 turret.Rotate(turret.up,eulers.y);
 gun.Rotate(gun.right,eulers.x);
 
 

Something along those lines might work.

Comment
Add comment · Show 2 · 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 oktemre · Jul 08, 2015 at 10:25 AM 0
Share

Nice solution but didnt match to my current situation.

In my current scene the tank structure is as below;

->Body -->EmptyGameObject --->Turret ---->Gun (the thin and long cylinder)

I dont need the gun part to be controlled separately.

I raycast the mouse click on the ground plane which gives me a point on XZ plane and when I use the lookat function to this point (which is my target) there is always a angle problem on turret when the tank body is not paralel to the ground (see the screenshot in the question). I need a correct rotation script for my empty game object or for a local rotation of my turret part to fix the issue.

avatar image Torigas · Jul 08, 2015 at 11:52 AM 0
Share

Then just take the turret part of the script I suggested?

avatar image
0

Answer by L_Artista · Jan 06, 2016 at 09:33 AM

 Vector3 aimVector = playerPosition - transform.position;
  aimVector.y = 0.0f;
  Quaternion newRotation = Quaternion.LookRotation(aimVector, transform.up);
  transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * rotateSpeed);

Considering it's a turrent which can only rotate in one axis that will be y! So rotate the turret in y axis only!

Follow this rts tutorial and most of your upcoming problems will be solved! http://stormtek.geek.nz/rts_tutorial/

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to reduce object materials ? 1 Answer

animation play on click 1 Answer

if (Input.GetKeyDown("w")) Not working 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