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 tonyenkiducx · Jun 23, 2013 at 01:35 PM · rotationworldrelative

Movement relative to world axis&local rotation

I've just started out with Unity, so apologies if this is a stupid question, but I've searched high and low for an answer.

I have a camera that is pointing downwards at a 45 degree angle(45,0,0), and it will always remain at this angle. You can rotate the camera around a fixed point(A fake gameObject following the focal point of the camera). The bit I am struggling with is the panning of the camera. I want it to pan across a fix plane, so it should not move up or down, only left, right, forward or backwards. Because of the rotation the left/right movement is just a world.self translate, but when I try to translate the forward/backward movement it moves relative to the camera and starts to move along both the Y and Z axis(Essentially zooming in).

I tried setting the translate to self.world, which works ok at the start, but when you rotate the camera it continues to move along worlds Z axis which is wrong.

So I want to pan across the Z world axis, but in a relative direction to the cameras rotation.

I hope I explained that all ok :) Here's the parts of my movement code that are relevant.

 transform.Translate(horzMoveAmt,0.0f,0.0f,Space.Self);
                 
 //Does not work when rotated        
 transform.Translate(0.0f,0.0f,vertMoveAmt,World.Self);
 
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

3 Replies

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

Answer by amphoterik · Jun 24, 2013 at 12:57 PM

You have two things that you can do. The first thing is to make the camera reset its x-axis rotation (0, 0, 0), then rotate it, and then rotate the x-axis back (45, 0, 0). Granted, this is a bit sloppy, but it will do what you want.

The second option is to actually nest the camera into the object that it is always looking at (or an empty object at the same location). then all you will need to do is rotate that object and the camera will rotate the way you want.

Comment
Add comment · Show 5 · 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 tonyenkiducx · Jun 24, 2013 at 05:00 PM 0
Share

I tried to do this, with this code..

             Quaternion tempRotate = transform.rotation;
             
             transform.rotation = Quaternion.Euler(0, tempRotate.y, tempRotate.z);
             
             transform.Translate(Vector3.forward * vert$$anonymous$$oveAmt);
             
             transform.rotation = tempRotate;

But no joy, it is still moving relative to the world and not to the forward face of the camera. I will have to give the second method a bash, but I'm unsure why this first method is not working, it was my fallback sloppy code solution as well.

avatar image amphoterik · Jun 24, 2013 at 05:04 PM 0
Share

Say the camera is 5 units away (along z) from the object in question and you want it to rotate 20 degrees. A sloppy way would be like:

 transform.Rotate(45, 0, 0); //reset it
 transform.Translate(0, 0, 5); //move it to center
 transform.Rotate(0, 20, 0); // rotate it
 transform.Translate(0, 0, -5); //move it back
 transform.Rotate(-45, 0, 0); look again

Like I said, very sloppy.

avatar image tonyenkiducx · Jun 24, 2013 at 05:17 PM 0
Share

I got it work with this is in the end..

             Vector3 tempRotate = transform.rotation.eulerAngles;
             transform.rotation = Quaternion.Euler(0, tempRotate.y, tempRotate.z);
             transform.Translate(0.0f, 0.0f,vert$$anonymous$$oveAmt, Space.Self);
             transform.rotation = Quaternion.Euler(tempRotate.x, tempRotate.y, tempRotate.z);


This makes the programmer in me cry, but it will have to do for now. Putting the camera inside a game object has brought up a whole host of other problems(The game object rotating slower than the camera), so I will stick with this until I'm more comfortable with Unity. Thanks for the help!

avatar image amphoterik · Jun 24, 2013 at 05:19 PM 0
Share

That's why I suggested an empty game object so that you can control the speed of rotation.

avatar image tonyenkiducx · Jun 24, 2013 at 10:51 PM 0
Share

I already marked as answered, I can't upvote yet unfortunately.

avatar image
1

Answer by s_guy · Jun 25, 2013 at 12:10 AM

Subtract the location of the focal point from the location of the camera. Moving +/- along this vector will move the camera closer or farther. Save this off (or don't reevaluate) if you want the camera to stay on the same plane.

The cross product between Vector3.up and the vector determined above will yield a vector orthogonal to both. Moving the camera +/- along this vector will pan it.

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
0

Answer by daverabbich · Jun 24, 2013 at 12:31 PM

I think you should put the Camera in a gameobject and move that instead of trying to move the camera

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

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

Related Questions

Rotate object in 90 degrees relative to camera. 1 Answer

Copy world rotation cordinate 1 Answer

Instantiate gun & arms rig relative to rotation & position of first person controller camera 0 Answers

Fake 2D world rotation by moving elements of the world 1 Answer

Apply a Rotation in World Space based on Quaternions 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