• 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 jimjames · Dec 27, 2014 at 05:46 PM ·

Adding a Roation Variable to Control Rotation Speed

I have this code that looks at a target with 2 axis's. Works great when all rigged up, but I would like to be able to control the rotation speed of both axis's. I think I would need to use a Lerp but not sure how to go about it. Add to this code or just have to come up with a different code.

CODE...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class TurretRotate //turrets variables to rotate and look at target, 2 axis
 {
     public Transform horizontalAxis;
     public Transform verticalAxis;
     public Transform currentTarget;
 }
 
 public class TurretControl : MonoBehaviour
 {
 void Update()
     {
                 LookAtTarget ();
         }
 void LookAtTarget()
     {
         Vector3 targetDirection = TR.verticalAxis.InverseTransformPoint(TR.currentTarget.position);// - verticalAxis.position;
         targetDirection.y = 0;
         targetDirection = TR.verticalAxis.TransformPoint(targetDirection);
         TR.verticalAxis.LookAt(targetDirection, TR.verticalAxis.up);
         TR.horizontalAxis.LookAt(TR.currentTarget,TR.horizontalAxis.up);
     }
 }


Thanks for your time in reading this question.

James

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 Scribe · Dec 27, 2014 at 07:01 PM

The ~easiest~ way that I can think of doing this is using a couple of the builtin Quaternion class methods, namely Quaternion.LookRotation which returns the rotation that LookAt will produce, and Quaternion.RotateTowards which moves between two rotations at a given speed.

 float rotationSpeed = 90f; //90degrees per second in each axis
 
 void LookAtTarget(){
     Vector3 targetDirection = TR.verticalAxis.InverseTransformPoint(TR.currentTarget.position);
     
     targetDirection.y = 0;
     
     targetDirection = TR.verticalAxis.TransformPoint(targetDirection);
     
     Quaternion vTarg = Quaternion.LookRotation(targetDirection, TR.verticalAxis.up);
     TR.verticalAxis.rotation = Quaternion.RotateTowards(TR.verticalAxis.rotation, vTarg, rotationSpeed*Time.deltaTime);
     
     Quaternion hTarg = Quaternion.LookRotation(TR.currentTarget,TR.horizontalAxis.up);
     TR.horizontalAxis.rotation = Quaternion.RotateTowards(TR.horizontalAxis.rotation, hTarg, rotationSpeed*Time.deltaTime);
 }



EDIT


so looking at your setup here is a revised script:

 public float rotationSpeed = 90f; //90degrees per second in each axis
 
 public Transform horizontalAxis; //rotate to tilt
 public Transform verticalAxis; //rotate to pan
 public Transform currentTarget;
 
 void Update(){
     LookAtTarget();
 }
 
 void LookAtTarget(){
     Vector3 panDir = currentTarget.position - horizontalAxis.position;
     panDir.y = 0;
     Quaternion vTarg = Quaternion.LookRotation(panDir, verticalAxis.up); //calculate the rotation around the vertical axis
     
     Vector3 curFwdDir = Quaternion.Inverse(verticalAxis.rotation)*horizontalAxis.forward; //find the local forward direction of the horizontalAxis
     
     Vector3 targTiltDir = currentTarget.position - horizontalAxis.position;
     targTiltDir.z = Mathf.Sqrt(targTiltDir.x*targTiltDir.x + targTiltDir.z*targTiltDir.z);
     targTiltDir.x = 0; //find the z-aligned direction of the target for comparison with curFwdDir (you can use Debug.DrawRay, if you want to see what these are doing)
     
     Quaternion hTarg = Quaternion.LookRotation(targTiltDir); //get the rotation solely on the horizontal axis
     
     //rotate over time at 'rotationSpeed' degrees per second
     verticalAxis.rotation = Quaternion.RotateTowards(verticalAxis.rotation, vTarg, rotationSpeed*Time.deltaTime);
     horizontalAxis.localRotation = Quaternion.RotateTowards(horizontalAxis.localRotation, hTarg, rotationSpeed*Time.deltaTime);
 }

There might be an easier way of doing this, the only bit I don't particularly like about this is calculating 'targTiltDir' but that is still ~mathematically correct~ so it shouldn't give any problems, just might not be such aesthetic code!

Hope that helps!

Scribe

Comment
Add comment · Show 6 · 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 Scribe · Dec 27, 2014 at 08:11 PM 0
Share

oops, line 13 should maybe be:

 Quaternion hTarg = Quaternion.LookRotation(TR.currentTarget.position-TR.horizontalAxis.position,TR.horizontalAxis.up);

for it to compile, though I have a feeling I may have got this wrong completely :O if you can give me a screenshot/picture of your setup with how you are using the vertical and horizontal transform that would be useful. I can then test some stuff out my end if this doesn't work! (Quaternions can get 'fun')

avatar image Scribe · Dec 27, 2014 at 08:16 PM 0
Share

or perhaps targetDirection as the first argument, it is hard to know without knowing the setup!

avatar image jimjames · Dec 27, 2014 at 09:13 PM 0
Share

Here is a pic. Don't $$anonymous$$d the model. It is for scripting purposes and will use my game model when this script gets working.

alt text

TURRET = empty game object with script and collider

"Pan Empty Game Object" has the same transform as "Base Point"

"Tilt Empty Game Object" has same transform as "head"

Really appreciate the help.

James

2 axis look at pic.jpg (439.9 kB)
avatar image Scribe · Dec 28, 2014 at 12:06 AM 0
Share

Check my edit!

avatar image jimjames · Dec 28, 2014 at 01:37 AM 0
Share

I want to say thank you so much for the amount of help you are providing, but the variable "rotationSpeed" has no effect on the rotation of the turret. Everything works great as for the 2 axis rotation but the turret just snaps towards the target as before.

Thank You again for the amount of effort you have put into to trying to solve this problem.

James

Show more comments

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

27 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

Related Questions

Source code for library function "RecalculateNormals()"? 4 Answers

Finding object via script 1 Answer

Respawning script question. 1 Answer

My code break character controller 2 Answers

Local Variables 2 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