• 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 tracyaf_2 · Dec 31, 2013 at 12:49 AM · rotationplayermouseoculusrift

Move player forward with mouse relative to camera

I am trying to get my character to move forward relative to the y camera rotation. I am working with the oculus rift using a mouse as the input. I've done this:

             if (Input.GetAxis("Mouse X")<0) moveLeft =true;
             if (Input.GetAxis("Mouse X")>0) moveRight =true;
             if (Input.GetAxis("Mouse Y")<0) moveBack =true;
             if (Input.GetAxis("Mouse Y")>0) moveForward =true;
             if (Input.GetKey(KeyCode.KeypadMinus)) Acceleration = Acceleration - .005f;
             if (Input.GetKey(KeyCode.KeypadPlus)) Acceleration = Acceleration + .005f;
         
             // Arrow keys
             if (Input.GetKey(KeyCode.UpArrow))    moveForward = true;
             if (Input.GetKey(KeyCode.LeftArrow))  moveLeft       = true;
             if (Input.GetKey(KeyCode.DownArrow))  moveBack       = true; 
             if (Input.GetKey(KeyCode.RightArrow)) moveRight   = true; 
             
 
     
 
             if (moveForward)
                 MoveThrottle += DirXform.TransformDirection(Vector3.forward * moveInfluence);
 
             if (moveBack)
                 MoveThrottle += DirXform.TransformDirection(Vector3.back * moveInfluence);
 
             if (moveLeft)
                 MoveThrottle += DirXform.TransformDirection(Vector3.left * moveInfluence);
 
             if (moveRight)
                 MoveThrottle +=  DirXform.TransformDirection(Vector3.right * moveInfluence);




The keys work properly, however the mouse movement moves the character on a locked x and y axis no matter the camera rotation. I am very poor at coding so please explain thoroughly.

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 HuskyPanda213 · Dec 31, 2013 at 01:43 AM 0
Share

I think the use of books may be causing it. Use two floats, one for mouse x and one for mouse y. Then make a dead zone control, so it Is not too sensitive. $$anonymous$$ove by using the floats multiplied by altitude.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zombience · Dec 31, 2013 at 01:54 AM

using Vector3.forward or Vector3.right, Vector3.left, etc, are all WORLD directions. So Vector3.forward will always be z positive according to the world coordinates.

instead use

 Camera.main.transform.forward

and

 Camera.main.transform.right

etc.

Replace all of your Vector3 directional code with references to the camera. You can access the main camera (the camera must be tagged as "Main Camera" in the inspector. When you start a new scene, the first camera defaults to this tag) through that line of code:

Camera (the camera class)

Camera.main (a static reference to the camera that is tagged as "Main Camera")

Camera.main.transform (the transform, i.e. the object that contains all position and rotation info, that is attached to the main camera object)

Camera.main.transform.forward (the "forward" direction that is relative to that transform, or basically the direction the camera is facing)

hope that helps

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 tracyaf_2 · Dec 31, 2013 at 02:24 PM 0
Share

That will work well enough for now, thanks!

avatar image zombience tracyaf_2 · Jan 01, 2014 at 07:13 PM 0
Share

for comments like this, post a new "comment", not an "answer" also, if the question is answered, don't forget to mark it as answered.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Rotating GameObject Around Player With "Mouse Y" Position 2 Answers

Vertical mouse input inverted when player is turned around 0 Answers

Tag Player in VRTK 1 Answer

Rotate object about x-axis with mouse like in editor 1 Answer

How to make a player rotate in a direction with a slope of the ground. 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