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
0
Question by AcidMage · Dec 19, 2012 at 03:38 PM · c#character controllerinput.getkeymovedirection

Moving along relative z axis with character controller?

Hi guys,

Essentially what I'm making is a hover-craft of sorts. I'm using both the Horizontal and Vertical axis for both the rotation and the up and down movement, relegating the forward movement to an acceleration button of sorts, in this case I'm going with the space bar. All is hunky-dory, except that there seems to be a problem with my script in that the object will move along the game world's Z, not its own. I know how to solve this with rigidbody, but I've found that using character controller is pretty handy for this game as the physics aren't exactly realistic. When I use axis for this, it works fine, so the problem appears to be in the Input.GetKey function.

Anyway, here is my script, hopefully someone has an answer for me?

 void Update () {
         CharacterController controller = GetComponent<CharacterController>();
         
             //move the object up and down
             moveDirection = new Vector3(0,Input.GetAxis ("Vertical"),0);
             moveDirection = transform.TransformDirection(moveDirection) * speed;
         
             //steer the object left and right
             transform.Rotate(0,Input.GetAxis ("Horizontal") * rotationspeed,0);
         
             //accelerate the object forward
             if(Input.GetKey(KeyCode.Space)) {
                 moveDirection = new Vector3(0,0,speed);
         }
             controller.Move(moveDirection * Time.deltaTime);
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by CodeMasterMike · Dec 19, 2012 at 03:56 PM

The InverseTransformDirection function transforms a direction from world space to local space. The TransformDirection function transforms a direction from local space to world space, so the other way around.

So, if your moveDirection is based on the world space, and you want the direction to be in the local space of the character, then you should use InverseTransformDirection.

InverseTransformDirection documentation.

Good luck!

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Rigidbody Keep Vertical Without Applying Rotational Constraints 2 Answers

Link button script to cam or character 1 Answer

Shift BHOP in Character Controller 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