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 vexe · Aug 11, 2013 at 06:16 PM · quaternionrotateaxisrotationscoordinate-system

Simulating 3D object rotation in a 2D space/system?

Hello, the title might be confusing, let me clear it out.

I'm trying to recreate the "Examine" feature in Resident Evil Remake but using the mouse instead of the arrow keys. I have found this script, but the problem with it, well... hard to explain by words, take a look at the pictures below. I'm gonna be using the arrows keys for simplicity's sake, here's the code for rotation:

 void Update()
 {
     horizRot -= Input.GetAxis("Horizontal") * speed * friction;
     vertRot += Input.GetAxis("Vertical") * speed * friction;
     fromRotation = transform.rotation;
     toRotation = Quaternion.Euler(vertRot, horizRot, 0);
     transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * lerpSpeed);
 }

Now take a look at the next pair of images to understand the problem: alt text

In the left image, pressing left/right will rotate the cube around the y-axis, to the right/left, which is exactly what's expected from it. Same thing for the right one, pressing up/down will rotate around the x-axis going up/down. Everything's good, so far...

Now look at this pair: alt text

In the left image, if you press left/right you will actually rotate around the z-axis, and not the 'y', which what the script should do, that way, it's actually doing 'what I want' and not 'what it should do', why is it doing that? - shouldn't it rotate around the y-axis?

In the right image, is the reason I asked this question, if you press up/right, you will actually rotate around the x-axis, which is 'what the script should do' and not 'what I want'! (the opposite of the previous situation) I want it to rotate around the z-axis!

I hope you understand what I'm trying to achieve now, and why did I say in my title 'simulate 3d object rotation in a 2d space/system' - because the way I want it, is rotate as if it's only a 2d system, no depth axis, as if we're not rotating in world space...

how can I achieve that? how can I rotate a 3d object in a 2d space?

Thanks for any help.

totalbadass.png (391.9 kB)
totalbadass.png (372.4 kB)
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

1 Reply

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

Answer by Joyrider · Aug 11, 2013 at 06:49 PM

You can't really rotate a 3D object in 2D space. But you can use a referential.

My guess is, you're always using the worlds axes, not the displayed local object's axes. (the transform.rotation is the world orientation of your object, with the world axes as referential) In your first example things just happen to coincide, in both cases.

What you could do, is

1/ create a dummy.

2/ Set it's position to that of the gameObject,

3/ and the orientation to that of your camera.

4/ Once that is done, set the dummy as a parent of your to-handle-object.

5/ And instead of using rotation, use localRotation.

That way, the referential aceswill always be Y for rotating it horizontaly, and X, for rotating it verticaly.

I guess it would be one of the "easiest" ways, otherwise you probably have to start playing around with quaterions.

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 vexe · Aug 12, 2013 at 03:31 AM 0
Share

Thank you so much for this idea! it worked nice, but not with my previous code, I used this.

 void Update()
 {
     float horizRot = -Input.GetAxis("Horizontal") * exa$$anonymous$$eRotateSensitivity.x * Time.deltaTime;
     float vertRot = -Input.GetAxis("Vertical") * exa$$anonymous$$eRotateSensitivity.y * Time.deltaTime;
     transform.RotateAround(transform.parent.up, $$anonymous$$athf.Deg2Rad * horizRot);
     transform.RotateAround(transform.parent.right, $$anonymous$$athf.Deg2Rad * vertRot);
 }
avatar image vexe · Aug 12, 2013 at 04:03 AM 0
Share

And even better than having a parent object, just rotate around the camera's up/right vectors!

avatar image vexe · Aug 12, 2013 at 04:04 AM 0
Share

But I'm having troubles with models pivot points, no matter how hard I try, they will always rotate around one pivot point, even if I create an empty game object and tell the model to rotate around that! any hacks around that?

avatar image vexe · Aug 12, 2013 at 04:08 AM 0
Share

Problem solved! - Created an empty game object for my model, made it a parent to the model, and ins$$anonymous$$d of attaching the script to the model, I attached it to the parent, so now I'm rotating the parent ins$$anonymous$$d, which in turn rotates the child model... :)

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

16 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Get my head around Unity Rotations 1 Answer

Rotate AI towards target, on single axis? 1 Answer

rotate an object on the z axis so that it does not exceed 0-45 degrees 0 Answers

UnityException: Input Axis Rotate is not setup. 2 Answers

Rotate object on the Y axis 90 degrees every 5 minutes? 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