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 love_and_lucky · Mar 29, 2016 at 06:42 PM · 2d gamecamera rotationrelative movement

Move player relative to camera rotation in 2D game

I am making a 2D game where the camera spins around with the player's rotation (so the z rotation of the camera and the player is always equal). I have been trying to get the player to move relative to the camera, so pressing up will always move it towards the top of the screen. I have seen plenty of fixes for this, but they are all for 3D games and I can't use anything involving a Vector3 or the Vector3.forward command because everything is in the x-y plane with Vector2s. I have tried using the angle of the camera's z rotation and putting into the following code that I got from the answer to a similar question but it didn't work at all:

 Vector2 Vector2FromAngle(float a)
 {
         a *= Mathf.Deg2Rad;
 
         return new Vector2(Mathf.Cos(a), Mathf.Sin(a));
 }

I tried using the above code on just the camera's angle, on the camera's angle made negative, and on the camera angle plus the result of Vector2.Angle(from, movement) where from was (0f, 1f) and then using the resulting Vector2 for movement. Nothing so far has worked. My movement code is

 //If the camera is rotated, move according to what the camera calls up
         if (cameraRotation != 0f && movement != new Vector2 (0f, 0f))
         {
             rb2d.AddForce(angleAdjust * speed);
         }
         //Otherwise, just use the basic axes
         else if (cameraRotation == 0f && movement != new Vector2(0f, 0f))
         {
             rb2d.AddForce(movement * speed);
         }

Where angleAdjust is the result of Vector2FromAngle using the sum of the rotation of the camera and the angle from the movement vector and movement is using the horizontal and vertical axes. I can't think of anything else that I could possibly do without changing the game to 3D and starting over. What can I do to make movement relative while still keeping the game 2D? Any suggestions are appreciated! Thank you!

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
0
Best Answer

Answer by love_and_lucky · Apr 20, 2016 at 05:42 PM

Fixed! It's actually a really simple solution - my entire movement code is now just

 `void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector2 movement = new Vector2(moveHorizontal, moveVertical);
 
         rb2d.AddRelativeForce(movement * speed);
     }`

I don't even need to factor in the camera's rotation. It works perfectly no matter what the rotation is.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Adjusting orthographic camera to work in landscape for Smartphone game 0 Answers

Script Error: OnCollisionEnter2D 1 Answer

Script to Hide player when H key is pressed 0 Answers

code so if 1 enemy isnt killed every 4 seconds player dies? 1 Answer

2D player enter/exit orbit 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