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
1
Question by jc_lvngstn 1 · May 31, 2010 at 07:12 PM · rotatetargetturretface

Rotating a turret body and head

The goal is to swivel the body of the turret (and thus the head) towards a target, and also tilt the head up and down to aim at the target.

The body swivels fine. however, the head refuses to swivel properly, it's like it is 90 degrees off. The axis of the head and body are all pointing the same direction initially. Here is my script:

// Handle base side to side rotation

 Vector3 localTarget = transform.InverseTransformDirection(Target.transform.position - transform.position);
 float targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
 float adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
 transform.Rotate(Vector3.up, adjustedAngle);

 // Handle head up/down swivel
 localTarget = Head.transform.InverseTransformDirection(Target.transform.position - Head.transform.position);
 targetAngle = Mathf.Atan2(localTarget.z, localTarget.y) * Mathf.Rad2Deg;
 adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
 Debug.Log(targetAngle);
 Head.transform.Rotate(Vector3.right, adjustedAngle); 

Here is a screenshot with the turret base (and thus the head, and everything attached), showing its orientation. This is when Unity is not running: alt text

Same thing, with just the 'Head' selected. Again, it has the same orientation as the base.

alt text

Here it is, after I run unity and tell it to face the small sphere: alt text

Any suggestions on why this is happening? I wonder if it has anything to do with the head being local to the body, so the target isn't really comparing against the 'real' coordinates?

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 jc_lvngstn 1 · May 31, 2010 at 07:13 PM 0
Share

And I seem to be too dumb to attach images. Anyone seeing screenshots, or just icons?

avatar image Keavon · Dec 31, 2010 at 07:26 PM 0
Share

All I see is "alt text"

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by privateknez · Sep 28, 2010 at 11:10 PM

    var targetAngle = Mathf.Atan2(localTarget.z, localTarget.y) * Mathf.Rad2Deg;
    var targetAngle2 = targetAngle - 90;
    var adjustedAngle = Mathf.Lerp(0, targetAngle2, Time.deltaTime * SwivelSpeed);

here is a snippet of your code in java, not the best solution but it works for me... i am trying for 3 days to make a turret like this... so thank you!

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 Max Kaufmann · Nov 04, 2010 at 07:31 AM 0
Share

I'd use $$anonymous$$athf.LerpAngle ins$$anonymous$$d of $$anonymous$$athf.Lerp to make sure you don't go the "long way around".

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

No one has followed this question yet.

Related Questions

Camera rotation around player while following. 6 Answers

problems with quaternion rotations 4 Answers

Make the turret automatically rotate to look at where the crosshair is aimed at. 3 Answers

make turret point where mouse points 3 Answers

Character always facing mouse cursor position... 3 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