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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by immerhart · Oct 17, 2012 at 10:42 PM · rotationturrettank

tank-tower rotation works unexpected

hey guys! i would like to have a tank-tower controll like in world of tanks. that means, you have a tower ontop of your tank which can rotate around the y-axis. and you have a gun that rotates with the tower and on the z-axis i think. alt text

(pillar has the script and gun is the verticalObj and suspension the horizontal) and here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponMover : MonoBehaviour
 {
     public Transform target;
     public GameObject verticalRotateObj;
     public GameObject horizontalRotateObj;
 
     public float maxVerticalRotationSpeed = 2.0f;
     public float maxHorizontalRotationSpeed = 1.0f;
 
     public float maxVerticalRotation = 30.0f;
     public float maxHorizontalRotation = 135.0f;
 
     private Vector3 m_newHorEuler;
     private Vector3 m_newVerEuler;
     private Vector2 m_currPos;
     private Vector2 m_targetPos;
     private float m_conversion = 0.0f;
     private float m_minVertAngle;
     private float m_maxVertAngle;
     private float m_minHorAngle;
     private float m_maxHorAngle;
     private float m_targetHorAngle;
     private float m_targetVertAngle;
 
     // Use this for initialization
     void Start()
     {
         m_conversion = 180/Mathf.PI;
         m_newHorEuler = Vector3.zero;
         m_newVerEuler = Vector3.zero;
         m_currPos = Vector2.zero;
         m_targetPos = Vector2.zero;
     }
 
     // Update is called once per frame
     void Update()
     {
         m_currPos.x = horizontalRotateObj.transform.position.x;
         m_currPos.y = horizontalRotateObj.transform.position.z;
         m_targetPos.x = target.transform.position.x;
         m_targetPos.y = target.transform.position.z;
         m_targetHorAngle = AngleToTarget(m_currPos, m_targetPos);
         m_newHorEuler.y = m_targetHorAngle + 90.0f;
         horizontalRotateObj.transform.eulerAngles = m_newHorEuler;
 
         m_currPos.x = verticalRotateObj.transform.position.y;
         m_currPos.y = verticalRotateObj.transform.position.x;
         m_targetPos.x = target.transform.position.y;
         m_targetPos.y = target.transform.position.x;
         m_targetVertAngle = AngleToTarget(m_currPos, m_targetPos);
         m_newVerEuler.z = m_targetVertAngle + 180.0f;
          m_newVerEuler.y = m_newHorEuler.y;
         verticalRotateObj.transform.eulerAngles = m_newVerEuler;
     }
 
     private float AngleToTarget(Vector2 objectPos, Vector2 targetPos)
     {
         return (Mathf.Atan2(objectPos.x - targetPos.x, objectPos.y - targetPos.y) * m_conversion);
     }
 }


works ok but the problem is, if the target moves over a certain line, the gun gets flipped 180° on z-axis. i have no idea why... ;( every help is welcome!!!

update:

     void Update()
 {
         Vector3 angle = Quaternion.LookRotation(target.position - horizontalRotateObj.transform.position).eulerAngles;
         angle.x = horizontalRotateObj.transform.eulerAngles.x;
         angle.z = horizontalRotateObj.transform.eulerAngles.z;
         angle.y -= 90.0f;
         horizontalRotateObj.transform.eulerAngles = angle;
 
         angle = Quaternion.LookRotation(target.position - verticalRotateObj.transform.position).eulerAngles;
         angle.x = verticalRotateObj.transform.eulerAngles.x;
         angle.y -= 90.0f;
         verticalRotateObj.transform.eulerAngles = angle;
     }






unbenannt.png (2.3 kB)
Comment
Add comment · Show 2
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 whydoidoit · Oct 17, 2012 at 11:43 PM 0
Share

This is a 2D game? I'm confused about the Vector2 for objectPos and targetPos

avatar image immerhart · Oct 18, 2012 at 08:10 AM 0
Share

no, its 3d. but for a rotation on one axis you need only two vector positions...

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Oct 18, 2012 at 08:41 AM

You'd be better off using the pivot point where the gun barrel attaches to the turret - then use

    var angles = Quaternion.LookRotation(targetPosition - attachmentPosition).eulerAngles;

You can then ignore the X rotation that you don't care about and use just Y and Z. Rotate the turret by the Y angle and elevate the gun by the Z angle - presuming that you aren't accounting for gravity in the projectile's path that should be a direct point.

Comment
Add comment · Show 3 · 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 immerhart · Oct 18, 2012 at 05:44 PM 0
Share

i made a update. the problem is that the gun doesn´t rotate in the z axis any more... -__-

avatar image whydoidoit · Oct 18, 2012 at 05:48 PM 0
Share

So Debug.Log the Quaternion you get from the rotation to make sure you are getting reasonable values for the rotations. It's probably just not applying them to the right axes.

avatar image whydoidoit · Oct 18, 2012 at 06:32 PM 0
Share

I've moved our comments around :)

avatar image
0

Answer by immerhart · Oct 18, 2012 at 06:08 PM

holy shit, i think it works. not sure what happens if i move the tank, yet...

Vector3 angle = Quaternion.LookRotation(target.position - horizontalRotateObj.transform.position).eulerAngles; angle.x = horizontalRotateObj.transform.eulerAngles.x; angle.z = horizontalRotateObj.transform.eulerAngles.z; angle.y -= 90.0f; horizontalRotateObj.transform.eulerAngles = angle;

angle = Quaternion.LookRotation(target.position - verticalRotateObj.transform.position).eulerAngles; angle.y -= 90.0f; angle.z = -angle.x; angle.x = verticalRotateObj.transform.eulerAngles.x; verticalRotateObj.transform.eulerAngles = angle;

you should answer the question and i give you the thumb =) the problem now: if the tank rotates in a diffrent axis. and i am not sure how to rotate it with a max speed... :|

Comment
Add comment · Show 3 · 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 immerhart · Oct 18, 2012 at 07:36 PM 0
Share

i dont get it. how can i add a rotation speed or something like that? a tower can´t rotate instant...!

avatar image whydoidoit · Oct 18, 2012 at 07:42 PM 0
Share

So rather than setting the angle create a new Quaternion and Slerp to it:

    var verticalTarget = Quaternion.Euler(angle);
    verticalRotateObj.transform.rotation = Quaternion.Slerp(verticalRotateObj.transform.rotation, verticalTarget, Time.deltaTime * someSpeedFactor);

You might find this tutorial useful.

avatar image immerhart · Oct 18, 2012 at 07:43 PM 0
Share

lol. i am just reading it right now, because i checked your profile =)

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

10 People are following this question.

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

Related Questions

How can I rotate a tank's turret back to the starting turret position? 2 Answers

question about turret on tank 1 Answer

Rotate on Z axis 2 Answers

rotating a tank turret while the tank is rotating 3 Answers

Rotating turret floats off tank in a sideways circle. Cannon floats out of turret when rotating up. 0 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