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 Pysassin · Mar 05, 2013 at 06:00 AM · rotationquaternionlookatturret

Vector3.Angle making my object oscilate...

I have a turret that is going to follow the mouse. The gun part of the turrent only goes up and down and the base it is attached to will spin. The gun I got to work no problem, but when I try to get the turret to spin the thing goes crazy and keeps going in a circle regardless if the mouse has moved. I mean if the mouse doesn't move at all the turret still does.

The code I have is:

baseObject.eulerAngles.z = Vector3.Angle(baseObject.position - rayHit.point, baseObject.forward);

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 Khada · Mar 07, 2013 at 04:40 PM 0
Share

If either robertbu's answer or my answer have helped you, can you please tick the circular grey tick icon on one/both of our answers? This lets others know that the question is answered and makes it more likely that others will want to answer your questions in the future.

avatar image Pysassin · Mar 11, 2013 at 04:25 PM 0
Share

Sorry RL kept me away from this for a while and I was unable to test it until now.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Khada · Mar 05, 2013 at 07:29 AM

 baseObject.rotation = Quaternion.SetLookRotation(
     (rayHit.point - baseObject.position).normalized,
     Vector3.up);
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 Pysassin · Mar 11, 2013 at 04:26 PM 1
Share

I used code Derived from this, still had to clamp the X and reverse the Y but I got something that overall works ty.

avatar image
1

Answer by robertbu · Mar 05, 2013 at 07:26 AM

A couple of issues. First, you are setting only one axis of eulerAngles. This can cause problems. See the note in the Transform.eulerAngles Reference. The second problem is that Vector3.Angle does not return a signed angle. If you need to, you can get absolute 2D angles of vectors if you project them onto a plane using Mathf.Atan2(). But I suggest a different approach for a turret.

  • Make sure the forward of your turret model points down positive Z. If it does not, you can compensate with an empty game object.

  • Get the point you want the turret to look at.

  • Make it's 'y' value of that point match the 'y' value of the turret.

  • Use Transform.LookAt() to rotate the turret.

Based on the line of code above, I'm assuming that rayHit.point is the point you want the turret base to point at:

 var v3Pos = rayHit.point;
 v3Pos.y = transform.y;
 transform.LookAt(v3Pos);


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

11 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

Related Questions

Quaternion Equivalent of restricting an axes rotation to zero 3 Answers

How to add a rotation to this? 1 Answer

How to clamp the rotation of this? 0 Answers

Using MouseLook script with LookAt function 2 Answers

problems with quaternion rotations 4 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