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
0
Question by jenapier · Aug 25, 2019 at 12:11 AM · movementplayercharactercontrollermove

How can I convert LOCAL transform.forward to WORLD Vector3?

Hi guys,

I'm using:

 transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0.0f));
         
 if(Input.GetKey(KeyCode.W)){
       transform.position += transform.forward * speed * Time.deltaTime;
 }
 if(Input.GetKey(KeyCode.S)){
       transform.position -= transform.forward * speed * Time.deltaTime;
 }

because I need the player to move relative to the direction they face. For instance if the player is facing North and presses 'W' (up), they move north. If the player is facing East and presses 'W' (up) they move East.

But I also need this to work with the CharacterController, so need to convert to a Vector 3 ('moveDirection').

 characterController.Move(moveDirection * Time.deltaTime);

Thoughts?

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 DCordoba · Aug 25, 2019 at 04:16 AM -1
Share

is the same vectorial space you dont need to convert to any other space, transform.forward are on global (or world) space so;

 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)){
        moveDirection = transform.forward * speed;
  }
  if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)){
        moveDirection = transform.forward * speed;
  }
 
  characterController.$$anonymous$$ove(moveDirection * Time.deltaTime);

should work.

to clarify what I think you're missing, note the absence of += this add the direction vector to the current point, you just need the direction so its just =

2 Replies

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

Answer by Hellium · Aug 25, 2019 at 01:12 PM

As indicated by DCordoba, transform.forward is already a vector defined in the World space.


What you really need is the vector expressed in the local space. In your case:

 Vector3 moveDirection;
 if(Input.GetKey(KeyCode.W)){
        moveDirection = Vector3.forward;
  }
  if(Input.GetKey(KeyCode.S)){
        moveDirection = -Vector3.forward;
  }
 characterController.Move(moveDirection * speed * Time.deltaTime);
Comment
Add comment · Show 4 · 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 sacredgeometry · Aug 25, 2019 at 01:23 PM 0
Share

Whilst I agree this is the best way to do this. i.e. just move the player up, down, left and right with w, s, a and d respectively and then just change the players orientation.

I am not sure just your code example fully answers the question. Which was albeit a little vague.

avatar image sacredgeometry · Aug 25, 2019 at 01:24 PM 0
Share

To expound on that. They asked how to have W always be the button used to move forward in multiple directions.

avatar image Hellium sacredgeometry · Aug 25, 2019 at 01:31 PM 1
Share

The CharacterController already takes care of the orientation of the object when moving it. So you just need to provide a local direction, and the $$anonymous$$ove will move the object according to the rotation of the object (the same way transform.Translate does)

avatar image sacredgeometry Hellium · Aug 25, 2019 at 01:32 PM 0
Share

Ah good point.

avatar image
0

Answer by sacredgeometry · Aug 25, 2019 at 12:41 PM

What you need to be doing is rotating the player and then forward will always be forward.

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

190 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 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 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 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 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

How do you move a character using a slider? 1 Answer

Problems with the movement of the player 1 Answer

Tweaking character movement 1 Answer

Moving AI away or towards player not working correctly. 1 Answer

How to hold objects in third person? 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