• 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
Question by Kyle13524 · Jul 23, 2015 at 04:14 AM · rotationvector3quaternionforwardeuler

Vector3.forward only moves me in 2 directions when rotating

So the way I am creating my character controller is very different from most mmorpg games. Part of the idea came from Outspark's Fiesta Online, where wasd all moved the character at a degree offset to the camera. For example running with W would move you forward at 0 degrees from the camera while running with D would move you forward at 90 degrees from the camera. This was really nice because you can run towards yourself and easily see all angles of you're character, along with being able to turn with the right mouse button.

For a better idea, this is how I worked it out in code:

         float cameraY = cameraController.transform.eulerAngles.y;
         float transformY = transform.eulerAngles.y;
         
         if (Input.GetKey("w"))
         {
             if (transformY != cameraY)
             { 
                 transform.eulerAngles = new Vector3(0, cameraY, 0);                
                 playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical"));
             }  
         }
 
         if (Input.GetKey("d"))
         {
             if (transformY != cameraY + 90)
             {
                 transform.eulerAngles = new Vector3(0, cameraY + 90, 0);
                 playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Horizontal"));
             }
         }
 
         if (Input.GetKey("s"))
         {
             if (transformY != cameraY + 180)
             {
                 transform.eulerAngles = new Vector3(0, cameraY + 180, 0);
                 playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical"));
             }
         }
 
         if (Input.GetKey("a"))
         {
             if (transformY != cameraY + 270)
             {
                 transform.eulerAngles = new Vector3(0, cameraY + 270, 0);
                 playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Horizontal"));
             }
         }

This works pretty well in the sense that the player stays at an offset to the camera, and that all works great. But for some reason it only moves me in 2 directions. When I press W it moves me forward, but when I press S it still moves me in the same direction. Same with D and A.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Veldars · Jul 23, 2015 at 06:13 AM

Hi,

I think this is because you rotate your player, you must go forward with the absolut value of "Input.GetAxis" (Mathf.Abs) because the direction will be define by "Vector3.forwarrd".

When you want to go back your forward is back but you multiply it by a negative nuber so your character go in the same direction than with "W"...

I hope this is clear...

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem with rotation around axis 1 Answer

Quaternion.Euler rotations don't add up 0 Answers

Quaternion.Slerp and Quaternion.Euler problem 1 Answer

Player rotation 0 Answers

Transform rapidly spins when flipped upside down. 2 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