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 SicariusFoxus · Jan 08, 2020 at 09:34 AM · rotation2d-platformerflipping

2D Platformer - Arm rotation / fliping problem

Hello,

I'am creating 2D platformer shooter game as my graduation project. However I came across some bugs which i cannot solve so I would really appreciate someone's help.

So I'am using this script to rotate players arm when aiming and when the hand exceeds 180° character is supposed to flip :

 void Update()
 {
     Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

     if (Mathf.Abs(rotationZ) > 90f && m_FacingRight)
     {
         Flip();
     }
     else if (Mathf.Abs(rotationZ) < 90f && !m_FacingRight)
     {
         Flip();
     }
     if (!m_FacingRight)
     {
         rotationZ += 180;
     }
     transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
 }

 private void Flip()
  {
      m_FacingRight = !m_FacingRight;

      Vector3 theScale = MyPlayer.transform.localScale;
      theScale.x *= -1;
      MyPlayer.transform.localScale = theScale;
  }

}

However I'am also using Flip function in my Character controller script for running left and right. It looks like this :

public void Flip()

 {
     m_FacingRight = !m_FacingRight;
     transform.Rotate(0f, 180f, 0f);
 }

Well and my problem is that these two Flip functions overlap.

It is creating bugs like this when facing left and rotating arm : alt text

This is how i would like it to work : alt text

I appreciate any help and advice, thank you.

snimka-obrazovky-30.png (8.6 kB)
snimka-obrazovky-32.png (4.5 kB)
Comment
Add comment · Show 2
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 unity_ek98vnTRplGj8Q · Jan 08, 2020 at 03:57 PM 0
Share

So, to clarify, do you want the body to always be facing the mouse position or the run direction? Because it sounds like you are trying to do both

avatar image SicariusFoxus unity_ek98vnTRplGj8Q · Jan 08, 2020 at 04:23 PM 0
Share

Hello,

I created something like Aim state which means my player is ai$$anonymous$$g only when holding right mouse button and he is only able to rotate arm in this state, however I still want him to be able to run correctly while ai$$anonymous$$g. Like ai$$anonymous$$g to the left side of the map and going backwards when moving right.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DrCox · Jan 08, 2020 at 04:17 PM

Hi, so since an arm rotation >180° is turning your character around, I feel like what you want is the character to just always face the mouse position. Let's say he's running to the right, but the mouse is left. This would mean, he's basically running backwards (might need different animation), pointing the arm to the left, right? If I understood correctly, I would just determine the body direction on whether the Mouse.x is lesser or greater than the Player.x and the arm can then just point at the mouse position.

Hope this helps. Cheers

Comment
Add comment · Show 4 · 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 SicariusFoxus · Jan 08, 2020 at 05:08 PM 0
Share

Hey,

Thanks for your advice, you understood my problem correctly but Im not sure how to implement your solution. What do you mean by $$anonymous$$ouse.x because I'am getting current mouse position in Vector3 type and Vector3 cannot be used as an argument in if statement. I could get Player.x position with "transform.position.x" but what do I execute after the IF ?

avatar image DrCox SicariusFoxus · Jan 08, 2020 at 07:30 PM 1
Share

Oh, no worries. Let me get more into detail. To deter$$anonymous$$e when to flip your character, you can compare the world position of the mouse with the character position, something like this:

 void Update()
 {
     var mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     if(mouseWorldPos.x < $$anonymous$$yPlayer.transform.position.x)
     {
         // mouse is left of $$anonymous$$yPlayer
         SetPlayerDirection(-1);
     }
     else
     {
         // mouse is right of $$anonymous$$yPlayer
         SetPlayerDirection(1);
     }
 }
 
 void SetPlayerDirection(int x)
 {
     var currentScale = $$anonymous$$yPlayer.transform.localScale;
     if($$anonymous$$athf.Sign(currentScale.x) != $$anonymous$$athf.Sign(x))
     {
         currentScale.x *= -1;
         $$anonymous$$yPlayer.transform.localScale = currentScale;
     }
 }

You can then remove the flip from you character controller, as the mouse position is the only thing changing the direction of your player.

avatar image SicariusFoxus DrCox · Jan 08, 2020 at 08:39 PM 0
Share

First of all thank you very much for explaining this to me,

so, I tried it and it kind of fixed the problem since im not using movement flip now however it made arm inverted. You can check in the video in this link.

https://gyazo.com/40d71d8b819f0be33294c7687bfc89ef

Show more comments

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

237 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[2D] Rotation flips back to the right side for 1 frame after rotation 0 Answers

localScale.x Affecting Player Translations 0 Answers

transform.localScal not flipping 0 Answers

2d Turret WORKS but is turned 90 degrees from the right way 2 Answers

How to rotate a 2d sprite around the center 1 Answer


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