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 JeppeNygaard · Dec 02, 2010 at 08:21 PM · turretlock

Gimple lock? ... help me please

Hi There,

alt text

I'm building a simple turret. It has a sphere-base which rotate around its own y-axis when I press the left/right arrows. Parented under the base is the cannon-pipe which rotates up and down, around its own x-axis when I press up and down.

as long as I don't rotate the base the cannon-pipe rotates perfectly up and down, but as soon as the base rotates the slightest bit the up/down movement of the cannon-pipe act's really strange - and all over the place.

I assume I'm having problem with world/local space but to be honest I have no clue why this happens and more important I can't figure out how to solve it - I hope someone can help me!?

Here are the script for the base:

var rotYMin : float = -0.5; var rotYMax : float = 0.5; var rotYSpeed : float = 10;

function Update () { var rotY : float = Input.GetAxis("Horizontal") Time.deltaTime rotYSpeed; transform.Rotate(Vector3(0,rotY,0), Space.Self); transform.rotation.y = Mathf.Clamp(transform.rotation.y, rotYMin, rotYMax); }

Here is the script for the cannon Pipe:

var rotXMin : float = 0.3; var rotXMax : float = 0.5; var rotXSpeed : float = 100;

function Update () { var rotX : float = Input.GetAxis("Vertical") Time.deltaTime rotXSpeed; transform.Rotate(Vector3(rotX,0,0), Space.Self); Debug.Log(transform.rotation.x); transform.rotation.x = Mathf.Clamp(transform.rotation.x, rotXMin, rotXMax); }

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

Answer by skovacs1 · Dec 02, 2010 at 09:11 PM

Your biggest problem is that you are trying to modify a quaternion directly. If you wanted to clamp the euler angle rotation, you would change transform.eulerAngles or transform.localEulerAngles, but they are prone to both Gimbal Lock (when combined with other rotations at the same time in the same space) and only ever return positive values between 0 and 360(inclusive), making your Clamp erroneous for negative angles as well. Gimbal Lock should only really happen when you combine several simultaneous rotations upon each other which in your case, since you are using local rotation for child and parent separately and rotating on only one axis within each local space, should not be a problem.

Working code would be something like this which calculates your rotation and clamps it before applying it:

var rotXMin : float = 45.0f; var rotXMax : float = 90.0f; var rotXSpeed : float = 100; private var rotX : float = 0.0f;

function Update () { rotX = Mathf.Clamp(rotX+Input.GetAxis("Vertical")*Time.deltaTime*rotXSpeed, rotXMin,rotXMax); transform.localEulerAngles.x = rotX; }

function Start() { rotX = transform.localEulerAngles.x; }

and

var rotYMin : float = -90.0f; var rotYMax : float = 90.0f; var rotYSpeed : float = 100; private var rotY : float = 0.0f;

function Update () { rotY = Mathf.Clamp(rotY+Input.GetAxis("Horizontal")*Time.deltaTime*rotYSpeed, rotYMin,rotYMax); transform.localEulerAngles.y = rotY; }

function Start() { rotY = transform.localEulerAngles.y; }

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
1

Answer by Jesse Anders · Dec 02, 2010 at 09:14 PM

The problems you're seeing here aren't related to gimbal lock.

Here is a forum thread that's sort of on the same topic. (It's not exactly the same problem, but some of the information presented there is likely to be relevant.)

One of the first problems I see is that you're trying to manipulate the elements of the quaternion that represents the object's orientation directly. Although this can sort of work correctly in some cases (or at least appear to), it's almost never (if ever) an appropriate solution. In short, don't modify the elements of transform.rotation or transform.localRotation directly; instead, use Transform.Rotate(), or assign a new quaternion value to the property as needed.

If you want to limit the rotation of the turret, there are other means of doing that. For example, you can work with the orientation in Euler-angle form, and limit the angles that are used for the 'pitch' and 'yaw' rotations.

Based on what you've posted, I'd recommend working with angles directly for this, and then assigning new values to transform.eulerAngles or transform.localEulerAngles as appropriate.

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 Proclyon · Dec 02, 2010 at 09:58 PM 0
Share

I have had some positive results with manually creating Quaternions , I did some digging had a bunch of math. And I gotta agree. Do it just the way Jesse Anders suggests here. I only use a raw value and leave the matrices alone , all three of them being different solutions for solving the same equation (which is needed for differentiating between 3 axes I believe)

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

Turret Lock On 1 Answer

Locking Rotation object on Z axis 1 Answer

Runny Turret Shot (Android) 0 Answers

Enemy not detecting player 1 Answer

How do i predict the position of my player for the turret to shoot at? 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