• 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 /
  • Help Room /
avatar image
0
Question by michalzoufaly14 · Apr 07 at 07:02 PM · rotationrotate object

Rotation of an object towards another, but only up and down


Hi,

I'm making a game where I have a tank and I need to move the gun so that it points at the target.


I already have the turret rotation movement figured out, but I need to figure out the gun movement. But only up and down. The sideways movement is what the turret does. Unfortunately, I found that LookAt needs at least two axes, otherwise the up and down direction only works in 90 degrees.
How can this problem be solved so that the cannon only rotates up/down and completely ignores left and right movement?


I've been drowning in the internet for days trying to find a solution to the problem, but to no avail. Thanks for the help.


 using UnityEngine;
 public class gunRotate : MonoBehaviour
 {   
     public GameObject target;
     void Start()
     {
         target = GameObject.Find("MousePosition");
     }
     void Update()
     {
         Vector3 targetPosition = new Vector3(
             target.transform.position.x,
             transform.position.y,
             target.transform.position.z);
 
         transform.LookAt(targetPosition);
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Apr 07 at 09:21 PM

TurretController.cs

 using UnityEngine;
 public class TurretController : MonoBehaviour
 {
     [SerializeField] Transform _chasis = null;
     [SerializeField] Transform _turret = null;
     [SerializeField] Transform _gun = null;
     [SerializeField] Transform _target = null;
     [SerializeField] float _turretDegreesPerSecond = 45;
     [SerializeField] float _gunDegreesPerSecond = 10;
     [SerializeField] Vector2 _gunRotationRangeX = new Vector2( -20 , 10 );
     void Update ()
     {
         Vector3 targetPos = _target.position;
 
         // turret control:
         Vector3 targetPosTurret = new Vector3( targetPos.x , _turret.position.y , targetPos.z );
         Quaternion turretRotationFinal = Quaternion.LookRotation(
             forward:    targetPosTurret - _turret.position ,
             upwards:    _chasis.up
         );
         float turretDegreesToFinal = Quaternion.Angle( _turret.rotation , turretRotationFinal );
         _turret.rotation = Quaternion.Lerp(
             _turret.rotation , turretRotationFinal ,
             ( _turretDegreesPerSecond / turretDegreesToFinal ) * Time.deltaTime
         );
 
         // gun control:
         Vector3 targetPosGun = _turret.InverseTransformPoint( targetPos );
         float gunAngleFinal = -Mathf.Atan2( targetPosGun.y , targetPosGun.z ) * Mathf.Rad2Deg;
         float gunAngleLimited = Mathf.Clamp( gunAngleFinal , _gunRotationRangeX.x , _gunRotationRangeX.y );
         _gun.localRotation = Quaternion.Lerp(
             _gun.localRotation ,
             Quaternion.Euler( gunAngleLimited , 0 , 0 ) ,
             Mathf.Abs( _gunDegreesPerSecond / gunAngleLimited ) * Time.deltaTime
         );
     }
 }


Transform hierarchy:

 + chasis
 | chasis mesh
 | + turret
 | | turret mesh
 | | + gun
 | | | gun mesh


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 michalzoufaly14 · Apr 08 at 03:58 PM 0
Share

Thanks a lot, it works. I would like to ask, how can I limit the rotation so that the gun doesnt rotate more than 10 degrees down and 20 degrees up?

Thank you

avatar image andrew-lukasik michalzoufaly14 · Apr 08 at 09:10 PM 0
Share

I updated the OP and added smooth rotation as well.

avatar image michalzoufaly14 andrew-lukasik · Apr 08 at 09:38 PM 0
Share

Thank you very much. It helped me a lot.

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

225 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do I rotate an object without affecting later rotation? 0 Answers

Simple Rotate Script won't work! 2 Answers

Help with suns rotation 0 Answers

Rotating on other axis 1 Answer

Rotating around another object without parent/child relation?? 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