Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ExplodingTiger · Jan 13, 2016 at 06:53 PM · translateworldspaceworld spacelocalspacelocal space

Move camera along 2 axes in WorldSpace

I have a camera which I want to move along 2 axes (x and z). In local space we have:

alt text

And since the camera object is rotated, in world space we have:

alt text

What I want to achieve is: to move the camera along 2 axes via user input - the camera is looking at the plane from above and by pressing keyboard buttons, the user can slide along the plane in 4 flat directions. This works perfectly by manipulating the transform.position.x and transform.position.y properties in local space.

I have the following problem though: the user can also rotate the camera object. By doing so, I can no longer manipulate the local space transform, since at certain rotations "up" begins to mean "down" and etc.

I have tried doing the following in world space:

 transform.Translate(transform.right * user_input, Space.World);

But this does not achieve what I want since transform.right grabs the red vector in world space.

What I want to do is: move the camera along the red and blue vectors in local space, but still preserve the coordinates in world space.

I'd be grateful if someone provided some input on this. Thanks a bunch!

camera1.png (27.8 kB)
camera2.png (26.2 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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Drayan · Sep 01, 2016 at 08:37 AM

You can simply use the eulerAngles.y of the camera transform. This way you only have the rotation around the Y axis, then you apply the same rotation to the movement vector.

 var camForwardAngle = Camera.main.transform.eulerAngles.y;
     
     var moveVector = Quaternion.Euler(new Vector3(0, camForwardAngle, 0)) * 
 new Vector3 (xAmount, 0, zAmount);

In case someone is interested ;)

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 revolute · Jan 15, 2016 at 03:05 AM

If you mean that up arrow key is simply moving camera in its facing direction, try transform.forward.

Transform.forward will always give you forward vector, meaning that for cameras, forward vector would be the direction the camera is looking at.

Combine transform.forward with your input and the camera would move forward whenever you press the right key.

eg:

 void Update(){
 transform.position = transform.forward * speed * Time.deltaTime + transform.position;
 }

this code would simply move the object forwards.

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 ExplodingTiger · Jan 15, 2016 at 11:56 AM 0
Share

Thanks a bunch for the response. Transform.forward didn't work for me because I didn't want the camera to move in the direction it was looking at (as shown in the second screenshot of my post - the camera is looking at the plane in world space, hence the forward vector is pointing at the plane). I wanted to move the camera parallel to the plane (as shown in the first screenshot - the "forward vector" is parallel to the plane, but it's in local space).

I ended up using an empty container game object as a parent to my camera. That way I can move the container game object in world space, achieving the desired camera movement.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved] Translating Camera on only X, Z axis on World Space, no matter what is the rotation 0 Answers

Idk what's wrong with my code.. 1 Answer

Problem with bullet projectiles direction 2 Answers

DragRigidbody - move object in Local Space, not World Space 1 Answer

Camera Limitations Using Inverse Vectors 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