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 /
  • Help Room /
avatar image
0
Question by mrlafle · Dec 01, 2015 at 04:27 PM · vector3local3rd person controller3rd person camera

How to convert a direction Vector3 from world to local space

I am trying to make a custom 3rd person controller with Javascript.

I am getting float values from the axes of an analog stick (Xbox controller):

     function Update() {
            var moveX:float = Input.GetAxis("LeftStickX");
            var moveY:float = Input.GetAxis("LeftStickY");
     }

And I am using these floats to determine which direction my character needs to face:

 function FixedUpdate() {
         if(moveX || moveY){
                   var direction:Vector3 = new Vector3(moveX, 0, -moveY);
                   //direction = cam.transform.InverseTransformDirection(direction);
                   transform.forward = direction;
         }
 }

This works great, but only from one camera angle. Since "direction" is in the world space, it doesn't match up with whatever direction my camera may be facing.

If I can just convert "direction" from world space to the camera's local space, I should be able to control the character from many different camera angles.

Does anyone know how I can do this easily?

I tried using the commented-out line 4, which almost worked, except the x and z values of "direction" were reversed when my camera was facing east or west. ("cam" is the gameObject where my camera is stored)

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 Besbes_Salma · Jan 04, 2017 at 11:23 AM 0
Share

i'm struggling with the same problem, did you find any solution ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Winterblood · Feb 13, 2016 at 07:22 PM

Try this (not tested and I write in C# so I may have made typos!):

  function FixedUpdate() {
          if(moveX || moveY){
                    var direction:Vector3 = new Vector3(moveX, 0, -moveY);  // Think of this as the dir in camera space
                    var t:Transform = cam.transform;   // Make a local copy so we can change it
                    t.position = Vector3.zero;  // We don't care about the position, only the orientation
                    direction = t * direction;  // Transform dir into world space
                    direction.y = 0.0f;  // Zero Y component so that facing is guaranteed to be horizontal even if camera is raised (unless directly overhead...but avoid that ;)
                    transform.forward = direction;
          }
  }
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

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

Related Questions

My character moves according to global Vectors 1 Answer

3rd person character controller rotation keeps resetting to 0 0 Answers

Object with fixed position but follows camera rotation 0 Answers

How to fix "the type or namespace ".." could not be found" in unity 0 Answers

3rd Person Movement / Camera Control Help or Advice Needed 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