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 Jessy · Feb 19, 2011 at 11:53 PM · rotationplanetranslateeulerangleslocal

How do you combine local and planar translation?

I have a camera, tilted on its local x axis, to look upon the world below at an angle. I want to move it around based on some input. However, I'm not sure what the best way is, to move it the exact way I want.

  • Movement needs to be in a plane parallel with the world's XZ plane.
  • X input needs to map to local X translation.
  • Y input needs to map to neither the local Z, nor a world axis. Instead, it needs to be along the vector that you get when you take transform.forward and rotate it along the local X axis to be parallel with the XZ plane.

Another way to do this would be to give the transform a parent. The parent would not be rotated in X. Movement along the parent's axes would be perfect. I just don't want the overhead and complexity of that.

My current idea is to take a vector that represents this "alternative local translation", and rotate it around the world's Y axis. I don't really like having to rely on Euler angles, but this seems to be working so far. Got a better solution? Is there some kind of general solution, and if so, would this or something else be faster, given the constraints?

transform.position += Quaternion.Euler(0, transform.eulerAngles.y, 0) * xyzVector;
Comment
Add comment · Show 1
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 Jesse Anders · Feb 20, 2011 at 02:35 PM 0
Share

Added some more to my answer below (in response to your comments).

1 Reply

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

Answer by Jesse Anders · Feb 20, 2011 at 10:44 AM

if you don't want to use parenting, then one way to compute the forward vector would be (assuming I'm understanding the question correctly):

var forward = transform.forward;
forward.y = 0f; // Adjust for programming language if needed
forward.Normalize();

This will give you the same result as rotating the forward vector about the local X axis so as to be parallel with the XZ plane, as you described. The only caveat is that it may fail if the camera is looking straight up or down or nearly straight up or down. If you know the X rotation will be within a 'safe' range though, this method should work.

Another method would be to actually counter-rotate the forward vector about the X axis, just as you described (although the normalization method described above is a little more straightforward, code-wise).

IMO, using parenting would probably be the cleanest and most straightforward solution, but the other methods under discussion should work as well.

[Edit: In response to comment below...]

If the concern is performance-related, I'd guess that all the methods under discussion would probably have a similar cost. (That's just a guess of course - you'd have to actually compare them to be sure.) So from that standpoint, I'd just go with whatever makes the most logical sense.

The only other option I can think of aside from what's already been discussed is to store the orientation of the object as a pair of angles (pitch and yaw), update those angles in response to user input, and then rebuild the orientation from scratch each update from those angles, e.g.:

transform.eulerAngles = new Vector3(pitch, yaw, 0f);

Using Euler angles in this way is perfectly suitable for this type of motion, and if you have the 'yaw' angle stored separately, you can then easily compute the 'XZ plane' movement vector as needed.

It sounds like your objection to the parenting method is mostly conceptual. If so, I'd argue that the parenting method actually makes perfect sense from both a functional and organizational standpoint. It's just like any other scenario where the orientation of one object is dependent on another; even the 'first-person controller' that ships with Unity (or shipped with previous versions, at least) makes use of parenting in exactly this way. This method can be used effectively any time one orientation needs to depend on and yet be independent from another (e.g. a 'banking' effect for a spacecraft, a 'bobbing' effect for a character, etc.).

In this case, you have two logically distinct behaviors; one object moves about in a plane that's parallel to the XZ plane, while another, related object follows the position and orientation of that object, but is free to rotate about the local x axis.

Anyway, that's the method I'd use. Failing that, I'd go with 'yaw' and 'pitch' angles. After that would come the other ideas that have been discussed, and after that I'm out of ideas :)

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 Jessy · Feb 20, 2011 at 01:55 PM 0
Share

That's fine, but requires renormalizing. Sorry, I should have mentioned that I had already thought of that and specifically didn't want to do it, for that reason. I assume Quaternion.Euler is doing something just as bad or worse. I don't think parenting is a good option, because I then have a game object in the scene that creates a hierarchy that is only useful for transformation, not organization, and I aim for both, with a focus on the latter, even. Having to waste space in the Hierarchy for two game objects, for this, is...ObjecTionable.

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

Rotating on a plane 2 Answers

Limit local rotation 6 Answers

Rotating Object on its Local World Axis 4 Answers

World and Local Axis out of Alignment 1 Answer

Limit local Rotations on one axis 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