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 /
avatar image
0
Question by TheIBPoliceman · Nov 22, 2011 at 04:52 PM · rotationinterpolationturrets

Aiming a turret of arbitrary orientation

Hey all,

I'm trying to create a turret aiming system for a third person space shooter, and am running into problems with aiming turrets on the ship.

Basically I have an aim object, moved by the mouse that the turrets should point at. So far so good, that's simple to do. Where I run into problem is another feature I want to have - I want the turrets to a specific firing arc.

 Ship GameObject
 -Turret Object (empty gameObject)
 --Turret Base 
 ---Turret Barrel

The turret base should be locked to the ship in the local X and Z axes, and only be able to rotate around the Y axis. I'll ignore the barrel stuff for the moment, but ideally it is locked in all but local X.

The Turret Base always has a local position and rotation of (0,0,0). This is so I can just set the transform's localRotation.x and .z parameters to 0 to lock it to the ship. Positioning is done with the empty parent Turret Object. I copy the Turret Object and position it on my ship at an arbitrary location and orientation (could be upside down , at an angle or whatever).

My current setup looks like this. In the update function I run AimBase() and AimBarrel(). If I'm correct, what this should do is get a rotation pointing at the aimTarget's position, then interpolate from current rotation to it using Quaternion.Slerp. Then I check the angle between that interpolated rotation and the turret's original rotation (which I initialize in Start() ), and see if that exceeds my maximum aim angle. If it does, I do nothing.

After all that, I set the euler angles of the turret in X and Z to their original values (again, initialized in Start() ) to clamp it to the ship.

 function AimTurretBase() {
     var origPos:Vector3 = turretBody.position;
     var targetPos:Vector3 = aimTarget.position;
     var rotGoal = Quaternion.LookRotation(targetPos - origPos);
     
     var actualGoal = rotGoal;
     
     var curRot = Quaternion.Slerp(turretBody.rotation,actualGoal,Time.deltaTime*turretTurnRate);
     
 
     
       if (turretRestricted) {
         if (Quaternion.Angle (baseRotation ,curRot) > turretBodyMaxAngle) {
         // if angle is greater than the maximum angle... 
            // Do nothing    
         } else {
             turretBody.rotation = curRot;
         }
        } else {
           turretBody.rotation = curRot;
       }
     // Lock to the rotation.
     turretBody.localRotation.eulerAngles.z = turretZLocked;
     turretBody.localRotation.eulerAngles.x = turretXLocked;    
 }

It seems logical to me, but it doesn't work in all situations. If the turret is on "top" of the ship, it seems to be fine, but if the turret is at any rotation other than that, it won't work. It traverses to some random location - sometimes it looks like it's trying to rotate the long way around.

Does anyone see something that I've missed? My idea is that the last locking bit is screwing it up as by careful debugging I have found that if the turret isn't locked to the ship it performs just fine.

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 pyroneil · Nov 27, 2011 at 08:47 PM

I think the problem is that aimTarget.position and turretBody.position are both in world coordinates, while you are setting a local rotation. I believe you want something like this:

var localAim = turretBody.transform.InverseTransformDirection(aimTarget.position); var rotGoal = Quaternion.LookRotation(localAim);

If this doesn't work, I would recommend not using LookRotation, and instead manually setting the euler angles using some trig.

var localAim = turretBody.transform.InverseTransformDirection(aimTarget.position); var rotGoal = Quaternion.Euler(localAim.x, localAim.z);

That might not be exactly correct, but hopefully that points you in the right direction.

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 TheIBPoliceman · Nov 28, 2011 at 01:44 AM 0
Share

Yep, transfor$$anonymous$$g that into local worked! Had to use InverseTransformPoint though, wasn't working with the direction for some reason.

avatar image
0

Answer by TheIBPoliceman · Nov 28, 2011 at 01:47 AM

Yep, That worked. Thanks tons! For anyone interested:

 // transform the aim position into local coords
     var localAim = turretBody.parent.InverseTransformPoint(aimTarget.position);
     var rotGoal = Quaternion.LookRotation(localAim);
     
 var curRot = Quaternion.RotateTowards(turretBody.localRotation,rotGoal,Time.deltaTime*turretTurnRate);
 

 var toCompare:Quaternion = turretBody.parent.localRotation;
 toCompare.eulerAngles.z = 0;
 
 //print(Quaternion.Angle (toCompare ,curRot));
 
   if (turretRestricted) {
     if (Quaternion.Angle (toCompare ,curRot) > turretBodyMaxAngle) {
     // if angle is greater than the maximum angle... 
        // Do nothing
             
     } else {
         turretBody.localRotation = curRot;
     }
    } else {
       turretBody.localRotation = curRot;
   }
 
 // always clamp the turret to the ship
 turretBody.localRotation.eulerAngles.z = turretZLocked;
 turretBody.localRotation.eulerAngles.x = turretXLocked;    
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

Trouble Rotating Full-Circle 0 Answers

How to smoothly switch from script controlled rotation to animator controlled rotation (and the other way around) 1 Answer

Interpolation of player's position over RPC calls 2 Answers

torque controlled smooth rotation 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