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 olas72 · Mar 29, 2013 at 11:56 PM · cameralocal space

Moving an object in its local X/Z relative to the camera orientation

alt text

I have an object that is aligned correctly with the ground plane and I want to move it along its local x/z axis but in a direction that is relative to the cameras orientation. Needs to be camera relative forward and left and right.

I'm having a hard time figuring out where to start. Any suggestions?

move_example.jpg (182.7 kB)
Comment
Add comment · Show 2
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 olas72 · Mar 30, 2013 at 03:51 AM 0
Share

That almost worked.

Had to set

 dir.y=0;

then it worked like a charm.

avatar image robertbu · Mar 30, 2013 at 04:16 AM 0
Share

If it answered your question, please click the checkmark next to the answer to close it out.

2 Replies

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

Answer by robertbu · Mar 30, 2013 at 12:15 AM

There are a couple of different ways to approach this problem. Below is one approach that I think is a bit easier to understand:

  var dir : Vector3;
  dir = Camera.main.transform.forward;  // forward
 // dir = -Camera.main.transform.forward;  // back
 // dir = Camera.main.transform.right;  // right;
 // dir = -Camera.main.transsform.left;  // left;

  dir.y = transform.position.y
  dir.Normalize();
  transform.Translate(dir * speed * Time.deltaTime);

The commented out lines are for the other three directions.

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 Armetron · Oct 12, 2016 at 12:40 AM 0
Share

Question, while it is true that this does work for forward, back, left, and right. What about diagonals? Since the camera is facing slightly down that means the the normalized direction will not be equally forward/back and left/right. The left/right will have a greater value than the forward/back

avatar image
1
Wiki

Answer by TranquilMarmot · Nov 05, 2013 at 01:50 PM

While the accepted answer works, I also wanted to rotate the object to face the direction it's moving in. I ended up coming up with this, figured I'd post it for others who stumble across this answer:

 private void RotateRelativeToCamera(Vector3 direction, Camera cam) {
     // rotate given direction by the camera's rotation
     Vector3 camDir = cam.transform.rotation * direction;
     
     // add result to object's location to get relative direction
     Vector3 objectDir = transform.position + camDir;
     
     // create quaternion facing direction
     Quaternion targetRotation = Quaternion.LookRotation(objectDir - transform.position);
     
     // constrain rotation to the Y axis
     Quaternion constrained = Quaternion.Euler(0.0f, targetRotation.eulerAngles.y, 0.0f);
     
     // slerp rotation
     transform.rotation = Quaternion.Slerp(transform.rotation, constrained, Time.deltaTime * this.RotationDamping);
 }


Then, in update() I have something like

 if(movingLeft)
     this.RotateRelativeToCamera(Vector3.left, Camera.main);

 if(movingForward)
    this.RotateRelativeToCamera(Vector3.forward, Camera.main);

 // etc...


And to make the object move forward,

 rigidbody.velocity = rigidbody.rotation * new Vector3(0.0f, 0.0f, this.MovementSpeed);
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

12 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Get Simultaneously three target image with vuforia 1 Answer

Field of view 1 Answer

Scene camera zooms in too quickly and crazy? 2 Answers

Measuring XYZ coordinates in Unity vs in real life. 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