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
1
Question by eXtremeTY · Mar 07, 2014 at 07:02 PM · 2dshootingaimaiming

Problem with gun rotation after flipping character (2D)

Guys I have the following problem. It's 2D platformer. I have a character with gun attached to it, and on gun I have script for rotating itself based on cursor position. I normally start game right sided and I want to make it so if the player rotates gun (with mouse) more than 85 degrees lets say, it should be flipped over and gun should be positioned at 180' - angle. It works, but I have another problem..

I want to do it both ways (ie when right sided and rotates gun upwards or downwards really hard, flip it, and also if it's left sided and rotates gun upwards or downwards really hard, flip it again). The problem is once I'm flipped, degrees are messed up and simply expression (angle>90) won't cut it, because if I only slightly rotate gun downwards, it's already 300 and it snaps even it that position..

Here are a few images to help:

alt text

alt text

alt text

alt text

The code is:

 if (GameControl.control.isFacingRight == true)
         {
             Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
             Vector3 dir = Input.mousePosition - pos;
             float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); 
 
             if (angle > 90 && !wasFlipped)
             {
                 transform.parent.gameObject.GetComponent<PlayerControllerScript>().Flip();
                 wasFlipped = true;
             }
         }
         else if (GameControl.control.isFacingRight == false)
         {
             Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
             Vector3 dir = Input.mousePosition - pos;
             float angle = 180f - (Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 
             if (angle > 90 && wasFlipped)
             {
                 transform.parent.gameObject.GetComponent<PlayerControllerScript>().Flip();
                 wasFlipped = false;
             }
         }

The problem, once again is, in the latter block, if I'm facing left I can't just ask if angle > 90, because if I move aim a tiny bit below horizontal axis I end up with 360 degrees and because it's bigger than 90 I automatically get flipped at waist level..

It's really hard for me to explain it like this, but I hope you guys understand my problem :/

Thanks!

normal scenario.png (6.3 kB)
normal scenario facing 85.png (5.0 kB)
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

Answer by SuperTasche · Jun 28, 2017 at 12:18 AM

Hello.

It's been a while and you probably already found the answer yourself. However I will give a code snippet in order to explain how I solved this issue, since I am implementing the same mechanics:

 if (rotationZ <= 90) {
                 transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
 } else if (rotationZ >= 270) {
                  transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
 }
 
 if (rotationZ > 90 && rotationZ < 270) {
                   FlipPlayer();
 }
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

22 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

Related Questions

I need help with aiming a ball. 2 Answers

Looking and firing towards mouse position, 2d game. 3 Answers

AI reflect shooting 0 Answers

Use the mouse to aim? 2 Answers

2D Aiming and Shooting at the mouse 3 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