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 mtoskamp · Oct 12, 2015 at 07:34 PM · movementcamera movementtransform.forward

transform.forward giving unwanted movement

Hi all,

I'm working on a Virtual Reality project with the oculus where the player should simply move through the environment and can look around and interact with some objects.

How this, the movement in this case, will work is that the player will press a button (for now that's "W") and then move in the direction the player is looking. I got that to work, sort of, with the following code (note: that I as well partially found on the internet):

     public float moveSpeed = 2f;
 
     void Update () {
     
         if (Input.GetKey (KeyCode.W))
         {
             transform.position += Camera.main.transform.forward * Time.deltaTime * moveSpeed;
         }
     }

Problem with this code is that the player can move into the air or through the ground as the .forward, z axis, rotates with the direction the camera is facing. How can I only use the Y axis rotation of the camera so that the transform .forward will never point towards the ground or into the air?

Or are there other, more efficient, ways that I should know of? I tried looking it up, but didn't find any solutions that worked for me so far.

Any advice would be highly appreciated.

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 Play Creatively · Oct 12, 2015 at 08:42 PM 0
Share

No idea if it works but try to toggle the Global/Local option which's right above the animation button above the Scene screen or right below the window tab.

1 Reply

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

Answer by Dave-Carlile · Oct 12, 2015 at 07:58 PM

You can just zero out the forward vector component that you want to ignore. If you're moving on the x/z plane then you can zero out y, so the movement can't happen in the y direction at all...

 Vector3 forward = Camera.main.transform.forward;
 forward.y = 0;  // zero out y, leaving only x & z
 transform.position += forward * Time.deltaTime * moveSpeed;
Comment
Add comment · Show 2 · 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 mtoskamp · Oct 12, 2015 at 08:18 PM 0
Share

Wow thank you so much for your extremely fast reply and also the perfect solution to my problem. Now that I see it I can't believe I thought of it myself! :) And like you said, if the player will not move on the Y axis it can just be zero'd out. So lets hope my colleagues don't change their $$anonymous$$ds now.

Once again thanks a lot for your time and help!

avatar image Dharmawangsa · Feb 07, 2016 at 01:09 PM 0
Share

Thanks you so much, this helps me too :)

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

33 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

Related Questions

Make the camera follow the highest player up but not down. 0 Answers

How to Move the Object According to Image tracking 0 Answers

How to make a NavMeshAgent move in the direction it is looking? 4 Answers

Any scrips that is like WOW or GW2 movement? 0 Answers

I have a little issue with top down movement using the camera orientation. 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