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 Steven · Nov 30, 2013 at 03:46 AM · worldspacelocalspacetransformdirection

Specific use of TransformDirection

         Vector3 direction = new Vector3(Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
         direction = _mainCamera.TransformDirection (direction);
         Vector3 velocity = direction * _moveSpeed;

I have a camera that is offset at an angle roughly 45 degrees around the y, and I want my character controller to move relative to the camera. This code is basically doing what I want it to, and I understand the basic use of TransformDirection, but having two objects referenced is making a local to world conversion into something else.

direction(input.GetAxis) seems to be worldspace. The cameras direction is local(?).

I don't understand what is going on with the _mainCamera.TransformDirection(direction) part. I want to set direction to the cameras direction, great. Why do I reference direction inside the brackets for the cameras TransformDirection?

So what I am (not)understanding about this is that it is setting direction, which is in worldspace, to the worldspace direction of the camera, which is in localspace, which is referencing the worldspace direction of direction.

Where am I going wrong here?

Comment
Add comment · Show 4
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 robertbu · Nov 30, 2013 at 03:48 AM 0
Share

You say, "This code is basically doing what...," so are you just asking how this code works, or is there a specific issue you are trying to solve?

avatar image Steven · Nov 30, 2013 at 04:03 AM 0
Share

Sorry, I am having a terrible time trying to figure out how to phrase my question. The code works as I assumed it should though it isn't quite right for what I need. I want to understand it better to try and adjust it to work correctly.

All the examples of TransformDirection I can find are just adjusting one localspace to worldspace. I have two objects referenced here, not sure how that plays into what is going on.

avatar image robertbu · Nov 30, 2013 at 04:22 AM 1
Share

But you don't have two objects references here...at lest not in the code above. Input.GetAxis() just gets input from the keyboard, mouse or joystick. This input is not married to any specific object. You turn it into a vector, but that vector is neither local or global. It could be either.

For example, imagine that the value of "Horizontal" is 0.0 and the value of "Vertical" is 1.0. That is the joystick is all the way forward, or you are hitting the up arrow key. 'direction' would be (0.0, 0.0, 1.0) which is the same thing as Vector3.forward. In terms of direction in world space, that is going straight towards positive 'z'. If I use this vector as a local coordinate (as I might with Transform.Translate()), then it represents the forward of the object.

Your line...

 direction = _mainCamera.TransformDirection (direction);

...translates the local coordinate acquired form the GetAxis() calls from local to global. So if your input was (0.0, 0.0, 1.0) as above, the 'direction' will become the forward of the camera in world space. That is it will be _mainCamera.forward. If you use your player in this calculated direction, the player will be moved in the direction the camera is facing.

avatar image Steven · Nov 30, 2013 at 04:35 AM 0
Share

Right I see. I was attributing the direction value to the game object when i should be thinking of it as just a vector. I really appreciate the clarification of the getaxis results too. Thank you very much Robertbu.

0 Replies

· Add your reply
  • Sort: 

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

how to set the rotation of a game object around the world axes ? (can't use transform.rotate) 1 Answer

Clamping rotation doesn't work when local axis is different from global 0 Answers

How do I make an object move on the world space rather than the local space? 1 Answer

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

How to use Transform.TransformDirection of another transform to affect a separate Transforms local rotation? 0 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