• 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 Patyrn · Feb 16, 2011 at 04:20 AM · rotationaim

Proper way to do Turret Rotation over time?

Digging around I see sample code using many different methods, some seemingly more concise and compact than others. What is the best way to rotate a turret around the Y axis to face a target over x period of time?

EDIT:

This is what I came up with:

var targetVector : Vector3 = target.transform.position - turret.transform.position;
var currentVector : Vector3 = turret.transform.rotation * Vector3.forward;
turret.transform.rotation = Quaternion.LookRotation(Vector3.Slerp(currentVector, targetVector, Time.deltaTime));

Seems to work well.

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
1
Best Answer

Answer by Patyrn · Feb 25, 2011 at 09:13 PM

This is what I came up with plucked from my turret class. Hopefully it helps someone.

function Update () { if (isVehicle && stateTracker.state != stateTracker.GROUNDED) { return; }

 // If enemy is set, turn turret
 if (target) {
     var clampHorizontal : Vector3 = new Vector3(0, 1, 1);
     var clampVertical : Vector3 = new Vector3(1, 0, 1);

     // Get the Quaternion value of the turret pointing at the target
     var currentTurretRotation : Quaternion = turret.transform.localRotation;
     turret.transform.LookAt(target.transform.position);
     var targetTurretRotation : Quaternion = turret.transform.localRotation;
     turret.transform.localRotation = currentTurretRotation;

     // Clamp it to only y axis
     currentTurretRotation.eulerAngles = Vector3.Scale(clampHorizontal, currentTurretRotation.eulerAngles);
     targetTurretRotation.eulerAngles = Vector3.Scale(clampHorizontal, targetTurretRotation.eulerAngles);

     // At what angle will we be aimed at target?
     horizontalFiringSolution = targetTurretRotation;

     // Rotate it over time
     turret.transform.localRotation = Quaternion.RotateTowards(currentTurretRotation, targetTurretRotation, horizontalSeekSpeed);


     var currentBarrelRotation : Quaternion;
     var targetBarrelRotation : Quaternion;
     switch (projectileType) {

     case ProjectileTypeEnum.Projectile:
         var solutionAngle = CalculateProjectileFiringSolution();

         // Not enough range
         if (solutionAngle != null) {
             verticalFiringSolution = new Quaternion();
             verticalFiringSolution.eulerAngles = new Vector3(solutionAngle, 0, 0);

             currentBarrelRotation = barrel.transform.localRotation;
             targetBarrelRotation = verticalFiringSolution;

             barrel.transform.localRotation = Quaternion.RotateTowards(currentBarrelRotation, targetBarrelRotation, verticalSeekSpeed);
         }
         break;
     case ProjectileTypeEnum.Beam:
         // Get the Quaternion value of the barrel pointing at the target
         currentBarrelRotation = barrel.transform.localRotation;
         barrel.transform.LookAt(target.transform.position);
         targetBarrelRotation = barrel.transform.localRotation;
         barrel.transform.localRotation = currentBarrelRotation;

         verticalFiringSolution = targetBarrelRotation;

         // Clamp it to only the x axis
         currentBarrelRotation.eulerAngles = Vector3.Scale(clampVertical, currentBarrelRotation.eulerAngles);
         targetBarrelRotation.eulerAngles = Vector3.Scale(clampVertical, targetBarrelRotation.eulerAngles);          

         // Rotate it over time
         barrel.transform.localRotation = Quaternion.RotateTowards(currentBarrelRotation, targetBarrelRotation, verticalSeekSpeed);
         break;
     }
 }

}

function CalculateMaximumRange() { var barrelTransform = gunMuzzlePoint.transform.position;

 var g = Physics.gravity.y;
 var y = barrelTransform.y;
 var v = projectileSpeed;
 var a = Mathf.Deg2Rad * 45;

 var vSin = v * Mathf.Cos(a);
 var vCos = v * Mathf.Sin(a);

 var sqrt = Mathf.Sqrt(vSin * vSin + 2 * g * y);

 return Mathf.Abs((vSin / g) * (vCos + sqrt));

}

function CalculateProjectileFiringSolution() { var targetTransform = target.transform.position; var barrelTransform = gunMuzzlePoint.transform.position;

 var y = barrelTransform.y - targetTransform.y;

 targetTransform.y = barrelTransform.y = 0;

 var x = (targetTransform - barrelTransform).magnitude;
 var v = projectileSpeed;
 var g = Physics.gravity.y;

 var sqrt = (v*v*v*v) - (g * (g * (x*x) + 2 * y * (v*v)));

 // Not enough range
 if (sqrt < 0) {
     return null;
 }

 sqrt = Mathf.Sqrt(sqrt);

 // DirectFire chooses the low trajectory, otherwise high trajectory.
 if (directFire) {   
     return Mathf.Atan(((v*v) - sqrt) / (g*x)) * Mathf.Rad2Deg;
 } else {
     return Mathf.Atan(((v*v) + sqrt) / (g*x)) * Mathf.Rad2Deg;
 }

}

function isTargetInFiringArc() { GetTarget();

 if (target && verticalFiringSolution != null)   {
     // How far are we from our firing solution.
     var horizontalOffset = Quaternion.Angle(horizontalFiringSolution, turret.transform.localRotation);
     var verticalOffset = Quaternion.Angle(verticalFiringSolution, barrel.transform.localRotation);

     var distanceToTarget = target ? target.transform.position   - transform.position : Vector3.zero;

     if (distanceToTarget.magnitude < minRange) {
         return false;
     }

     // Doesn't work too well looking for 100% perfect accuracy, so substitute .1
     if (deviation == 0 && horizontalOffset < .1 && verticalOffset < .1) {
         return true;
     } else if (horizontalOffset < deviation && verticalOffset < deviation) {
         return true;
     }
 }

 return false;

}

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 DaveA · Feb 16, 2011 at 04:24 AM

http://unity3d.com/support/documentation/ScriptReference/Vector3.RotateTowards.html or http://unity3d.com/support/documentation/ScriptReference/Vector3.Slerp.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 Patyrn · Feb 16, 2011 at 04:41 AM 0
Share

Could you include an example snippet for RotateTowards? I don't understand how to use it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Getting the face direction 2 Answers

Rotate player to aim on one axis (Z-axis) towards mouse position/joystick - 2.5D (3D) 0 Answers

The angle in forward axis between two points. 1 Answer

Aiming to gameobject 2 Answers

Freeze Camera Rotation that is attached to the object in certain moments 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