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
2
Question by Oninji · Oct 14, 2010 at 07:19 PM · rotationjavascriptangleaim

Problem with Z rotation when rotating X and Y and the same time.

Hellow.

When I rotate X and Y and the same time. The Z angle also rotate. Which offset the Cannon position.

The purpose of the script is to change a Cannon angle on the horizontal and the vertical. As to let the player aim.

Here is the snippet of code.

var speedPos = 40; var speedNeg = -40;

function Update () {

 if(Input.GetButton("keyup")){
 transform.Rotate(Time.deltaTime * speedPos, 0, 0, Space.Self);
 }

 if(Input.GetButton("keydown")){
 transform.Rotate(Time.deltaTime * speedNeg, 0, 0, Space.Self);
 }

 if(Input.GetButton("keyright")){
 transform.Rotate(0, Time.deltaTime * speedPos, 0, Space.Self);
 }

 if(Input.GetButton("keyleft")){
 transform.Rotate(0, Time.deltaTime * speedNeg, 0, Space.Self);
 }

}

Thank you.

Edit : Here to be clearer.

Here is the first state of the cannon.

http://img409.imageshack.us/img409/1865/state01.jpg

But when you play around and move it around.

http://img708.imageshack.us/img708/9760/state02.jpg

It become tilted, due to the Z rotation axis is moved, even though Z is never ordered to move in the code. Z generally move when X and Y rotation values are both moved.

I hope it's clearer, the computer I'm working on do not allow me to put in on video. So if it's still not comprehensible, I'll capture a video later.

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 Oninji · Oct 18, 2010 at 04:57 PM

I've corrected the Error. I've create 2 control, one for vertical and one for horizontal rotation. On the object, I've set a parent to my Cannon. Set the Vertical control on the cannon and horizontal control to the parent. Set the relative to Self, and it work like a Charm. :)

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
3

Answer by skovacs1 · Oct 14, 2010 at 08:08 PM

Because rotation is an operation dependent upon the origin and therefore the previous operations which change your origin, it is more challenging to order operations in this way and even more troubling when you consider the problem of gimbal lock. When you rotate about the x axis, the y and z axes will move and likewise, when you rotate about the y axis, the x and z axes will move.

This is why many rotations use world or parent space axes rather than local axes as they will not change as you rotate your object. Performing your rotations in world space (Space.World or eulerAngles) or about parent axes (localEulerAngles) should resolve your issue, depending on your setup.

To overcome the challenge of cumulative rotations throwing your axes off, the simple answer is to not have cumulative rotations and to simply set the rotation explicitly each time:

cannonAim.js

var speed : float = 40.0f; private var eulers : Vector3;

function Start() { eulers = transform.eulerAngles; }

function Update() { eulers.x -= Input.GetAxis("Vertical") speed Time.deltaTime; eulers.y += Input.GetAxis("Horizontal") speed Time.deltaTime; transform.eulerAngles = eulers; } //Because we perform a new rotation and override the old one, //there are no cumulative rotation problems.

Comment
Add comment · Show 4 · 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 Oninji · Oct 14, 2010 at 09:58 PM 0
Share

Well for now i've set a transform

transform.eulerAngles.z = 0;

to world space it doesn't work well, but how I'd it to the parent relative to the parent Axe exactly?

avatar image skovacs1 · Oct 14, 2010 at 10:26 PM 0
Share

As I describe in the answer, rotations would use a constant coordinate system. Simply setting eulerAngles.z = 0 will definitely fix your world z rotation (likely what you are having problems with offsetting position), but that is still not correct. You would need to update all of your rotations to use the same coordinate system for correct behaviour. Also, as I describe in the answer, the simple way to use the parent axes would be localEulerAngles.

avatar image Oninji · Oct 14, 2010 at 11:29 PM 0
Share

Even with the code you have give, it still give the same offsetting to the Z rotation axis, tilting the cannon.

I gave more explanation in the original question and supported it with images, hoping it is more comprehensible now.

avatar image skovacs1 · Oct 15, 2010 at 02:40 PM 0
Share

Sorry about that. I missed the compounded error of cumulative rotation still. I have corrected the script to avoid that problem.

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Applying force with respect to angle with the ground? 1 Answer

Calc angle. 1 Answer

Look at an angle 1 Answer


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